Reputation: 1447
Are there the ways how implement this idea:
I wanna do some check before render. If check is true than render. But if check is false, than firstly i need to do setState
, and only after render.
Thanks.
Upvotes: 0
Views: 3590
Reputation: 9
Try rendering it first, then do the check and change the state when it is already rendered by using componentDidMount()
. Don't worry, it will be that fast that your eyes won't see it coming. The code will looks like this:
class MyApp extends React.Component {
componentDidMount() {
if (CHECK HERE) {
this.setState({state : stateValue})
}
}
}
Upvotes: 0
Reputation: 1226
I think componentWillMount()
is literally what you are after but putting it in the constructor()
might be a better place for it.
class MyComponent extends React.Component {
constructor(props) {
super(props);
if (theCheck() === false) {
this.state = {
// ...
}
}
}
}
Upvotes: 2