state = {one_book : []}
updateName = event =>{
var newvalue = event.target.value
const id = 1;
let one_book = this.state.one_book; // create the copy of state array
one_book[1] = newvalue; //new value
this.setState({ one_book });
console.log(this.state.one_book)
}
这里新值是我们要更改为json文件的更新值。我想用这个新名称或用one_book更新json文件中整个对象的ID = 1的书名。更改应反映在原始json文件中]
I have to update this state into json file with the respective id.
Here is my json file
[
{
"id": 1,
"bookname": "Physics",
"price": 600,
"author": "ABC",
"pages": 567,
"category" : "School Books"
},
{
"id": 2,
"bookname": "Let Us C",
"price": 1300,
"author": "XYZ",
"pages": 1267,
"category" : "Technical"
}
]
状态= {one_book:[]} updateName =事件=> {var newvalue = event.target.value const id = 1;让one_book = this.state.one_book; //创建状态数组的副本...
此let one_book = this.state.one_book;
不会创建数组的副本。 Javascript数组和对象作为引用存储在变量中。
updateName =事件=> {var newvalue = event.target.valueconst id = 1;