javascript - 无法更新组件中的组件(`App`)错误
问题描述
我有以下组件:
import React, { useState, useEffect, useContext } from 'react';
import PropTypes from 'prop-types';
import { Redirect } from 'react-router-dom';
import DashboardContext from '../../contexts/DashboardContext';
import authorizeWorker from '../../workers/authorize-worker';
/**
* A protected route that calls the authorize API and redirects to login
* on fail
* @param {Function} component The component to redirect to on success
*/
const ProtectedRoute = ({ component }) => {
const [isAuthenticated, setIsAuthenticated] = useState(null);
const dashboardContext = useContext(DashboardContext);
dashboardContext.setIsDashboard(true);
const Component = component;
useEffect(() => {
authorizeWorker({})
.then(() => setIsAuthenticated(true))
.catch(() => setIsAuthenticated(false));
}, []);
if (isAuthenticated === true) {
return <Component />;
}
if (isAuthenticated === false) {
return <Redirect to="/login" />;
}
return null;
}
ProtectedRoute.propTypes = {
component: PropTypes.func
};
export default ProtectedRoute;
我将它用于我的路由器,例如
<ProtectedRoute path="/projects" component={Projects} />
我最近在控制台中看到一个警告:Warning: Cannot update a component (
App ) while rendering a different component (
ProtectedRoute ). To locate the bad setState() call inside
ProtectedRoute , follow the stack trace as described
。为什么我会看到此错误,我该如何解决?
解决方案
该错误是因为在初始渲染阶段,您渲染组件setIsDashboard(true);
,通常,您希望在安装时执行它(useEffect
使用空的 dep 数组)。
确保它setIsDashboard
是持久的,这意味着它是由 React API 创建的(如useState
)。
或者用useMemo
/来记忆它,或者你会得到 inifite 循环,因为在每次渲染时都会创建useCallback
一个新的实例,并且 dep 数组 ( ) 将导致另一个执行。setIsDashboard
[setIsDashboard]
const ProtectedRoute = ({ component }) => {
const { setIsDashboard } = useContext(DashboardContext);
// Make sure `setIsDashboard` persistant
useEffect(() => {
setIsDashboard(true);
}, [setIsDashboard]);
...
};
推荐阅读
- pywinauto - 如何从 Pywinauto 中的组编辑策略中获取 ActionMenuItem 的转储树
- r - 使用自己的规范在 R 中使用贝叶斯网络模拟数据
- python - Python PIL - 调整图像大小
- triggers - vscode 的菜单切换触发器不可操作
- windows - 是否可以在 Windows 10 中试用 Microsoft Accessibility MSAA?
- python - cmds.confirmDialog,帮助 Maya 的“问号”按钮配置支持
- docker - 我无法使用 Github 操作在 Github 内部 docker 注册表中发布图像
- visual-studio - Visual Studio 编辑器间歇性地变得无响应
- sequelize.js - Sequelize 在 N:M -> 1:M 关联中包含嵌套的实际对象
- javascript - 使用 require react js 没有出现本地图像