Answer the question
In order to leave comments, you need to log in
How to correctly check Props before rendering a component and make a redirect?
There is a container in which props are thrown through redux and which in the render renders a component with these props.
The problem is that you need to make the output of the component conditional, if there are props, then this is an object and render it. If there are no props, then instead of the object null and redirect via [email protected] to the main one (browserHistory.push('/')).
I do the check and redirect in componentWillMount(). I understand this so that if we got to this place and the component has not yet been mounted, then rendering should not occur after the redirect command, respectively, there should be no errors about reading props that they are null. But obviously I'm wrong. How is it done correctly?
And for backfill: as I wrote above, I use either a filled object {...} or null. I do not use PropTypes at the moment, because the type is not checked. But is it right to do so with an eye to using PropTypes?
Answer the question
In order to leave comments, you need to log in
The componentWillMount method is deprecated. React-router 4 has been out for a long time.
You can use the constructor and history.push, or render and the Redirect component.
Instead of PropTypes, it's better to use TypeScript or Flow.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question