Answer the question
In order to leave comments, you need to log in
Why is Warning: Cannot update during an existing state transition (such as within `render`) popping up in the console?
I'm trying to do an authorization check in an app written with React Router v4.
The code looks like this:
...
<Route render={(props) => {requireAuth(props); return <LoggedInLayout/>;}} />
//Перед тем, как перейти на определенный роут, проходит процесс проверки авторизовал ли пользователь или нет
...
function requireAuth(props) {
if (!SessionStore.isLoggedIn() && props.location.pathname.indexOf('login') === -1) {
props.history.location.state = {nextPathname: props.location.pathname};
props.history.replace('/login');
}
}
Warning: Cannot update during an existing state transition (such as within `render`). Render methods should be a pure function of props and state. react-dom.development.js?61bb:506
"props.history.replace('/login');"
produces this Warning. Answer the question
In order to leave comments, you need to log in
<Route
render={props => isRequireAuth() ? <Redirect to={{pathname: "/login", state: {nextPathname: props.location.pathname}}}/> : <LoggedInLayout/>}
/>
function isRequireAuth(props) {
return !SessionStore.isLoggedIn() && props.location.pathname.indexOf('login') === -1
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question