Answer the question
In order to leave comments, you need to log in
React and react-router, how to block the execution of a route?
Tell me, there is such a router code:
function requireTest(nextState, replaceState,cb) {
// псевдокод
Если нет ID новости {
cb();
}
иначе {
не вызываем колбек
}
}
export default (
<Route path="" component={App}>
<IndexRoute component={Index} />
<Route onEnter={requireTest} path="test/(:id/)" component={Test} />
</Route>
)
Answer the question
In order to leave comments, you need to log in
I'm not sure I fully understand the problem, but if it helps - very similar to what I did to check the user (logged in or not):
path: '/portal',
getComponent(location, cb) {
System.import('containers/Portal').then(loadModule(cb)).catch(errorLoading);
},
onEnter: (nextState, replace) => checkLoggedInState(store, nextState, replace),
childRoutes: [
{
path: 'dashboard',
...
},
path: 'products',
...
},
...
function checkLoggedInState(store, nextState, replace) {
if (!isLoggedIn(store)) {
replace({
pathname: '/login',
state: {nextPathname: nextState.location.pathname}
})
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question