Answer the question
In order to leave comments, you need to log in
How to make friends React-Router onEnter and redux?
I can not figure out the correct authorization in any way.
What should the function that checks the possibility of visiting a certain page by the user look like.
In stock:
Answer the question
In order to leave comments, you need to log in
A question of concern to many. I will describe two methods.
1
There is an alternative to onEnter: a component that checks whether to show content or show "access denied", for example. I spied the method itself here , if you understand redux deep enough, then everything is clear.
If not, then I should finish the routing mini-tutorial sometime and come back to this answer.
2
In order to pass "store" in onEnter, you need to wrap the routes in a function.
Looks like this:
export function getRoutes(store) {
return (
<div>
<Route path='/' component={App}>
<IndexRoute component={Home} />
<Route path='/admin' component={Admin} onEnter={Admin.onEnter.bind(this,store)}/>
<Route path='/genre/:genre' component={Genre}>
<Route path='/genre/:genre/:release' component={Release} />
</Route>
<Route path='/list' component={List} />
</Route>
<Route path='*' component={NotFound} />
</div>
)
}
export default class Admin extends Component {
static onEnter(store, nextState, replace) {
const user = store.getState().user
if (!user) {
replace('/')
}
}
render() {
return (
<div className='row'>
<div className='col-md-12'>Раздел /admin</div>
</div>
)
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question