Kirill Stas Kirill Stas - 1 month ago 10
React JSX Question

React: Execute some check before render

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.

Answer

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 = {
        // ...
      }
    }
  }
}