Sarkis Arutiunian Sarkis Arutiunian - 1 year ago 98
Javascript Question

React doesn't remap Array from state

Basically we have list component, something like this:

{this.state.answers[0] ? =>
<li key={answer._id}>{answer.text}</li>) : null}

And when I update state component is re rendering but mapped list stay the same even when in
new, different data, list use old data.
Is that some kind of bug or what? Why list doesn't remap not after updating state not even after re rendering?

Answer Source

I moved mapped element, into separate component which use states and handlers from parent element and now it works.

{this.state.answers[0] ? =>
         _setRecommended={this._setValueForRecommended.bind(this, answer.displayOrder)}
         _setExclude={this._setValueForExclude.bind(this, answer.displayOrder)}
    />) : null}

It works because all props "besides handlers" in Answer I keep in his own state and use componentWillReceiveProps where I update states, to force re rendering for each mapped element, and finally it works.

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