Accessing Props Value Inside Constructor

react-native, reactjs

Solution

You should pass in the `showModal` value in your parent component. Otherwise `props.showModal` will not show up in your constructor. ​

For example: `<MyComponent showModal={true} />` ​ Then, you'll be able to see: ​

constructor(props) {
    super(props);

    this.state = {
      show: props.showModal, // true
    };
}

Hope this helps

Problem

I am using the following code to try and set the initial state by accessing the props value. I thought I could do it by accessing the (props) value but It seems to be empty. ``` constructor(props) { super(props); //this.renderRowForMain = this.renderRowForMain.bind(this); this.state = { show: props.showModal, }; } ``` if I simply place the following insider the render() then it seems to be loading fine. ``` this.state = { show: this.props.showModal, }; ``` What I am trying to do is initially set the showModal state to true and then when a close button is tapped change the state to false.

Original source

Related problems