Предположим, что у нас есть вызов API, который аутентифицирует пользователя, а затем на основании того, возвращается ли пользователь с сервера или нет, мы либо заменяемState, либо ничего не делаем, что может выглядеть примерно так:`replaceState` не работает внутри` then` of Promise
<Route component={App} onEnter={authUser} />
const onEnter = (nextState, replaceState) => {
if (isClient) {
AuthHelper.findCurrentUser()
.then(response => {
// This here doesn't work, `replaceState` doesn't change the page or URL, however I know for sure
// that the line is being called because if I put a `debugger` right before it,
// the `debugger` hits every time...
replaceState({nextPathname: nextState.location.pathname}, '/dashboard');
})
.catch(response => {});
// If we put the same line here, it redirects the user to the dashbaord.
// replaceState({nextPathname: nextState.location.pathname}, '/dashboard');
}
}
Единственным фактором дифференциации между replaceState работает и не работает в том, что один вызывается из тогдашних обещания, и один называется вне разрешения Promise. Выполняет ли Promise действие replaceState?
DOH. Большое вам спасибо, сэр. Однако я должен сказать, что это не было в документах React Router, поэтому я был очень смущен. –
https://github.com/rackt/react-router/blob/master/ Docs/API.md # onenternextstate-replacestate обратного вызова – taion