Home  >  Article  >  Web Front-end  >  What should I do if the react view does not update?

What should I do if the react view does not update?

藏色散人
藏色散人Original
2022-12-29 10:21:032102browse

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.

What should I do if the react view does not update?

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 (
     
); } 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 (
     
); } 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!

Statement:
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
Previous article:How to delete node in nvmNext article:How to delete node in nvm