What should I do if the react array page does not refresh?

藏色散人
Release: 2023-01-18 14:33:30
Original
1639 people have browsed it

The react change array page does not refresh because the assignment of the array is passed by reference. The solution: 1. Open the corresponding file; 2. Find "data.push(obj)"; 3. Use the new array "let data = [...this.state.data];" is enough.

What should I do if the react array page does not refresh?

The operating environment of this tutorial: Windows 10 system, react18.0.0 version, Dell G3 computer.

What should I do if the page does not refresh when changing the array in react?

React array changes do not cause view updates

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: &#39;100%&#39;}}
           columns={this.state.columns}
           data={this.state.data}
       />
       <Button type="primary" onClick={this.addData.bind(this)}>添加</Button>
     </div>
    );
  }
  addData () {
    let obj = {
      date: &#39;2018-05-07&#39;,
      name: &#39;小明&#39;,
      address: &#39;&#39;
    };
    let data = this.state.data;
    data.push(obj);
    this.setState({
      data: data
    });
    console.log(this.state);
  }
}
export default App;
Copy after login

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 the virtual dom of react finds that the data in the state has not changed. , so the view is not updated, and a new array can be used at this time:

 let data = [...this.state.data];
Copy after login

The code is changed to:

import React, { Component } from &#39;react&#39;;
import &#39;./App.css&#39;;
import Todo from &#39;./components/todo/index&#39;
import { Table, Button } from &#39;element-react&#39;;
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: &#39;2016-05-02&#39;,
        name: &#39;王小虎&#39;,
        address: &#39;上海市普陀区金沙江路 1518 弄&#39;
      }, {
        date: &#39;2016-05-04&#39;,
        name: &#39;王小虎&#39;,
        address: &#39;上海市普陀区金沙江路 1517 弄&#39;
      }, {
        date: &#39;2016-05-01&#39;,
        name: &#39;王小虎&#39;,
        address: &#39;上海市普陀区金沙江路 1519 弄&#39;
      }, {
        date: &#39;2016-05-03&#39;,
        name: &#39;王小虎&#39;,
        address: &#39;上海市普陀区金沙江路 1516 弄&#39;
      },{
        date: &#39;2016-05-02&#39;,
        name: &#39;王小虎&#39;,
        address: &#39;上海市普陀区金沙江路 1518 弄&#39;
      }, {
        date: &#39;2016-05-04&#39;,
        name: &#39;王小虎&#39;,
        address: &#39;上海市普陀区金沙江路 1517 弄&#39;
      }, {
        date: &#39;2016-05-01&#39;,
        name: &#39;王小虎&#39;,
        address: &#39;上海市普陀区金沙江路 1519 弄&#39;
      }, {
        date: &#39;2016-05-03&#39;,
        name: &#39;王小虎&#39;,
        address: &#39;上海市普陀区金沙江路 1516 弄&#39;
      }]
    }
  }
  render() {
    return (
     <div>
       <Todo list={this.state.data}/>
       <Table
           style={{width: &#39;100%&#39;}}
           columns={this.state.columns}
           data={this.state.data}
       />
       <Button type="primary" onClick={this.addData.bind(this)}>添加</Button>
     </div>
    );
  }
  addData () {
    let obj = {
      date: &#39;2018-05-07&#39;,
      name: &#39;小明&#39;,
      address: &#39;&#39;
    };
    let data = [...this.state.data];
    data.push(obj);
    this.setState({
      data: data
    });
    console.log(this.state);
  }
}
export default App;
Copy after login

In this way, after data comparison, react will detect the new and old changes and update the dom

Recommended learning: "react video tutorial"

The above is the detailed content of What should I do if the react array page does not refresh?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template