I'm facing a wired issue when adding a new item to the start of the array
checkout the example
class Demo extends React.Component {
constructor(props) {
super(props);
this.state = {arr: [{title: 3}, {title: 2}, {title: 1}]};
}
render() {
return <div>
{
this.state.arr.map((item, index) => <Element title={item.title} isNew={item.isNew}
key={index}/>
)}
<button onClick={() => {
const arr = [...this.state.arr];
const newItem = this.state.arr.length + 1;
arr.unshift({title: newItem, isNew: true});
this.setState({arr: arr});
}}>ADD ELEMENT TO START
</button>
</div>
}
}
class Element extends React.Component {
constructor(props) {
super(props);
console.log('constructor : ', this.props.title);
}
componentDidMount() {
console.log('componentDidMount : ', this.props.title);
}
render() {
return <span style={{padding : '0 8px' , color: this.props.isNew ? 'red' : 'black'}}>{this.props.title}</span>
}
}
ReactDOM.render(<Demo/>, document.getElementById('root'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<div id="root"></div>
checkout the log when adding a new item to the start of the array, it shows the props of the last item of the array, not the first (since we add the item to the start).
I'm changing the state as required, the rendering is correct but the constructor of the wrong component(the last component) gets called the same goes for componentDidMount.
the app will work perfectly when adding the item to the end of the array but not the opposite.