I am trying to render a list of components in order with react, the component is updating this array of elements but is not re-ordering them.
Pseudo code;
class Form extends Component {
//
// .... other initialization code and logic
//
updatePositions() {
//
// re-order this.state.page.page_contents
//
this.setState({ page: this.state.page });
}
renderContents() {
return this.state.page.page_content.map((c, i) => {
return (<ContentItem
key={ i }
content={ c }
/>);
});
}
render() {
return (
<div className="row">
<div className="medium-12 columns">
{ this.renderContents() }
</div>
</div>
);
}
}
If i log out the results of page.page_content the items are being reordered in the array, however the form render is not re-rendering the contents in its new order
this.setState({ page: this.state.page })it implies you're mutating the state, if you want to change the state to a derived version of the previous state use:this.setState(prevState => ....).this.setState(prevState => ({page: [...prevState.page].sort(.....)}))