The react view is not updated because the assignment of the array is passed by reference. The solution is: 1. Open the corresponding react file; 2. By using "let data = [...this.state.data] ;" The new array method can be used to update the view.
The operating environment of this tutorial: Windows 10 system, react18.0.0 version, Dell G3 computer.
What should I do if the react view does not update?
React array changes do not cause the view to be updated
import React, { Component } from 'react'; import './App.css'; import Todo from './components/todo/index' import { Table, Button } from 'element-react'; class App extends Component { constructor(props) { super(props); this.state = { columns: [ { label: "日期", prop: "date", width: 180 }, { label: "姓名", prop: "name", width: 180 }, { label: "地址", prop: "address" } ], data: [{ date: '2016-05-02', name: '王小虎', address: '上海市普陀区金沙江路 1518 弄' }, { date: '2016-05-04', name: '王小虎', address: '上海市普陀区金沙江路 1517 弄' }, { date: '2016-05-01', name: '王小虎', address: '上海市普陀区金沙江路 1519 弄' }, { date: '2016-05-03', name: '王小虎', address: '上海市普陀区金沙江路 1516 弄' },{ date: '2016-05-02', name: '王小虎', address: '上海市普陀区金沙江路 1518 弄' }, { date: '2016-05-04', name: '王小虎', address: '上海市普陀区金沙江路 1517 弄' }, { date: '2016-05-01', name: '王小虎', address: '上海市普陀区金沙江路 1519 弄' }, { date: '2016-05-03', name: '王小虎', address: '上海市普陀区金沙江路 1516 弄' }] } } render() { return ( <div> <Todo list={this.state.data}/> <Table style={{width: '100%'}} columns={this.state.columns} data={this.state.data} /> <Button type="primary" onClick={this.addData.bind(this)}>添加</Button> </div> ); } addData () { let obj = { date: '2018-05-07', name: '小明', address: '' }; let data = this.state.data; data.push(obj); this.setState({ data: data }); console.log(this.state); } } export default App;
In the above code, the value of data is set through setState and the view is not updated. The reason is that the assignment of the array is Passed by reference, data = this.state.data is the memory used to execute the data array, so executing data.push(obj) is actually equivalent to executing this.state.data.push(obj), so react's virtual dom finds The data in the state has not changed, so the view is not updated. At this time, a new array can be used:
let data = [...this.state.data];
The code is changed to:
import React, { Component } from 'react'; import './App.css'; import Todo from './components/todo/index' import { Table, Button } from 'element-react'; class App extends Component { constructor(props) { super(props); this.state = { columns: [ { label: "日期", prop: "date", width: 180 }, { label: "姓名", prop: "name", width: 180 }, { label: "地址", prop: "address" } ], data: [{ date: '2016-05-02', name: '王小虎', address: '上海市普陀区金沙江路 1518 弄' }, { date: '2016-05-04', name: '王小虎', address: '上海市普陀区金沙江路 1517 弄' }, { date: '2016-05-01', name: '王小虎', address: '上海市普陀区金沙江路 1519 弄' }, { date: '2016-05-03', name: '王小虎', address: '上海市普陀区金沙江路 1516 弄' },{ date: '2016-05-02', name: '王小虎', address: '上海市普陀区金沙江路 1518 弄' }, { date: '2016-05-04', name: '王小虎', address: '上海市普陀区金沙江路 1517 弄' }, { date: '2016-05-01', name: '王小虎', address: '上海市普陀区金沙江路 1519 弄' }, { date: '2016-05-03', name: '王小虎', address: '上海市普陀区金沙江路 1516 弄' }] } } render() { return ( <div> <Todo list={this.state.data}/> <Table style={{width: '100%'}} columns={this.state.columns} data={this.state.data} /> <Button type="primary" onClick={this.addData.bind(this)}>添加</Button> </div> ); } addData () { let obj = { date: '2018-05-07', name: '小明', address: '' }; let data = [...this.state.data]; data.push(obj); this.setState({ data: data }); console.log(this.state); } } export default App;
In this way, after comparing the data, react will detect the new and old ones. Update dom with changes
Recommended learning: "react video tutorial"
The above is the detailed content of What should I do if the react view does not update?. For more information, please follow other related articles on the PHP Chinese website!