user3900157 user3900157 -4 years ago 110
React JSX Question

React meteor - setup state based on props

I'm developing a React + Meteor app.

I have a route that accepts a parameter for example to show the single page of a document, so the route accepts an ID parameter. Then the route forwards the request to:

createContainer component that gets the data from collection (based on id) and return the document to a child component

child component displays data

Now, in my child component, I want to setup state in the constructor based on the value from the data (document), but the value of the data is stil undefined in the constructor, is this normal (still loading)? How can I setup state based on value of data?

Thanks.

Answer Source

Yes, it's normal.

You need to look at componentWillReceiveProps(). Compare nextProps with this.props and set the state.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download