警告:渲染不同组件时无法更新组件.ReactRedux.Consumer [英] Warning: Cannot update a component while rendering a different component. ReactRedux.Consumer
问题描述
我在我的网络应用程序中收到一条警告,并且已经阅读了很多关于该问题的帖子.不幸的是,我还没有设法解决我的问题,希望有人能给我一些建议.据我所知,我需要找到一种在 useEffect 中分派到商店的方法.但到目前为止,我的努力都没有成功.
I am getting a warning in my web app and have read a lot of posts about the issue. Unfortunately I have not managed to resolve my problem and hope someone might have some advice. From what I can tell I need to find a way of dispatching to the store in a useEffect. But my efforts so far have been unsuccessful.
警告说:
index.js:1 警告:在渲染不同的组件 (ReactRedux.Consumer
) 时无法更新组件 (Connect(TabMenuComponent)
).要在 ReactRedux.Consumer
中找到错误的 setState() 调用,请按照 https://reactjs.org/link/setstate-in-render
index.js:1 Warning: Cannot update a component (
Connect(TabMenuComponent)
) while rendering a different component (ReactRedux.Consumer
). To locate the bad setState() call insideReactRedux.Consumer
, follow the stack trace as described in https://reactjs.org/link/setstate-in-render
堆栈跟踪进一步将我指向此文件.它指向第 30 行,即 store.dispatch 行:
The stack trace further points me to this file. It points to line 30 which is the store.dispatch line:
export const AuthRoute = ({ component: Component, roles, computedMatch, ignoreRedirection, ...rest }) => (
<Route exact {...rest} render={props => {
return <ReactReduxContext.Consumer>
{({ store }) => {
const user = store.getState().appData.user;
if (!user) {
auth.setRedirectUrl(window.location.pathname);
return <Redirect to={auth.loginUrl} />;
}
const redirectUrl = auth.getRedirectUrl();
if (redirectUrl && !ignoreRedirection) {
auth.removeRedirectUrl();
return <Redirect to={redirectUrl} />;
}
if (roles && roles.length && !roles.some(neededRole => user.roles.some(userRole => userRole === neededRole))) {
return <BaseLayout authError={stringKeys.error.unauthorized}></BaseLayout>;
}
store.dispatch({ type: "ROUTE_CHANGED", url: computedMatch.url, path: computedMatch.path, params: computedMatch.params })
return <Component {...props} />;
}}
</ReactReduxContext.Consumer>;
}
} />
);
推荐答案
您在渲染过程中分派了一个不正确的动作.你应该做的是为你的输入组件创建一个 HOC 或一个包装组件,并在你的组件安装后分派动作
You are dispatching an action in the middle of a render which is not correct. What you should instead do is to create an HOC or a wrapper component to your input Component and dispatch the action once thee component is mounted
使用类组件包装器:
class CompWithDispatch extends React.Component {
componentDidMount() {
const { store, type, url, path, params } = this.props;
store.dispatch({ type, url, path, params })
}
render() {
const { store, type, url, path, params , component: Component, ...rest} = this.props;
return <Component {...rest} />
}
}
带函数组件包装器
const CompWithDispatch = (props) => {
const { store, type, url, path, params, component:Component, ...rest } = props;
useEffect(() => {
store.dispatch({ type, url, path, params })
}, []);
return <Component {...rest} />
}
并像使用它一样
export const AuthRoute = ({ component, roles, computedMatch, ignoreRedirection, ...rest }) => (
<Route exact {...rest} render={props => {
return <ReactReduxContext.Consumer>
{({ store }) => {
const user = store.getState().appData.user;
if (!user) {
auth.setRedirectUrl(window.location.pathname);
return <Redirect to={auth.loginUrl} />;
}
const redirectUrl = auth.getRedirectUrl();
if (redirectUrl && !ignoreRedirection) {
auth.removeRedirectUrl();
return <Redirect to={redirectUrl} />;
}
if (roles && roles.length && !roles.some(neededRole => user.roles.some(userRole => userRole === neededRole))) {
return <BaseLayout authError={stringKeys.error.unauthorized}></BaseLayout>;
}
const additionalProps = { type: "ROUTE_CHANGED", url: computedMatch.url, path: computedMatch.path, params: computedMatch.params })
return <CompWithDispatch {...additionalProps} {...props} component={component}/>;
}}
</ReactReduxContext.Consumer>;
}
} />
);
这篇关于警告:渲染不同组件时无法更新组件.ReactRedux.Consumer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!