Im new to ReactJS, I'm trying to update input value to a different one however, for some reason it is not updating as it should be, I've got the code on stackblitz for more explanation if you want to try it out as it is performing a very strange behaviour. Appreciate your assistance.
Stackblitz: https://stackblitz.com/edit/easy-drag-and-drop-fxs217?file=index.js
import React, { Component } from 'react';
import { render } from 'react-dom';
import './style.css';
class App extends Component {
constructor() {
super();
this.state = {
myArray: [
[123, 364],
[469, 983]
]
};
this.handleInputChange = this.handleInputChange.bind(this);
}
handleInputChange(e, index) {
let updatedState = [...this.state.myArray];
console.log("updatedstate ", updatedState)
console.log(e.target.value)
updatedState[index] = [Number(e.target.value), this.state.myArray[index][1]]
this.setState=({
myArray: updatedState
})
}
render() {
return (
<div>
{this.state.myArray.map((item, index) => (
<input key={index} type="text" value={item[0]} onChange={(e) => this.handleInputChange(e,
index)} />
))}
</div>
);
}
}
render(<App />, document.getElementById('root'));
this.setState()method.handleInputChangefunction you need to update the state like so:this.setState({ myArray: updatedState});. P.S. the spread operator doesn't do DEEP clones, and currently your function actually mutates the state array. In this case it won't really be an issue, but in some instances can cause buggy behaviour so just keep it in mind=afterthis.setState. Also make sure it'smyArray: updatedStateas you have it here. Your stackblitz snippet works if you do that.