Home Web Front-end Front-end Q&A What should I do if the react array page does not refresh?

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

Jan 18, 2023 pm 02:33 PM
react array

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Integration of Java framework and front-end React framework Integration of Java framework and front-end React framework Jun 01, 2024 pm 03:16 PM

Integration of Java framework and React framework: Steps: Set up the back-end Java framework. Create project structure. Configure build tools. Create React applications. Write REST API endpoints. Configure the communication mechanism. Practical case (SpringBoot+React): Java code: Define RESTfulAPI controller. React code: Get and display the data returned by the API.

Application of PHP array grouping function in data sorting Application of PHP array grouping function in data sorting May 04, 2024 pm 01:03 PM

PHP's array_group_by function can group elements in an array based on keys or closure functions, returning an associative array where the key is the group name and the value is an array of elements belonging to the group.

The role of PHP array grouping function in finding duplicate elements The role of PHP array grouping function in finding duplicate elements May 05, 2024 am 09:21 AM

PHP's array_group() function can be used to group an array by a specified key to find duplicate elements. This function works through the following steps: Use key_callback to specify the grouping key. Optionally use value_callback to determine grouping values. Count grouped elements and identify duplicates. Therefore, the array_group() function is very useful for finding and processing duplicate elements.

Vue.js vs. React: Project-Specific Considerations Vue.js vs. React: Project-Specific Considerations Apr 09, 2025 am 12:01 AM

Vue.js is suitable for small and medium-sized projects and fast iterations, while React is suitable for large and complex applications. 1) Vue.js is easy to use and is suitable for situations where the team is insufficient or the project scale is small. 2) React has a richer ecosystem and is suitable for projects with high performance and complex functional needs.

Can arrays be used as function parameters? Can arrays be used as function parameters? Jun 04, 2024 pm 04:30 PM

Yes, in many programming languages, arrays can be used as function parameters, and the function will perform operations on the data stored in it. For example, the printArray function in C++ can print the elements in an array, while the printArray function in Python can traverse the array and print its elements. Modifications made to the array by these functions are also reflected in the original array in the calling function.

React's Role in HTML: Enhancing User Experience React's Role in HTML: Enhancing User Experience Apr 09, 2025 am 12:11 AM

React combines JSX and HTML to improve user experience. 1) JSX embeds HTML to make development more intuitive. 2) The virtual DOM mechanism optimizes performance and reduces DOM operations. 3) Component-based management UI to improve maintainability. 4) State management and event processing enhance interactivity.

PHP array key-value exchange: Analysis of the advantages and disadvantages of common algorithms PHP array key-value exchange: Analysis of the advantages and disadvantages of common algorithms May 04, 2024 pm 10:39 PM

Three common algorithms for exchanging array key values ​​in PHP have their own advantages and disadvantages: array_flip(): simple and efficient, but the values ​​must be unique and cannot handle multi-dimensional arrays. Manual traversal: can handle multi-dimensional arrays and control exceptions, but the code is longer and less efficient. ksort()+array_keys(): can handle any type of array and control the sort order, but it is less efficient. Practical cases show that array_flip() is the most efficient, but when dealing with multi-dimensional arrays, manual traversal is more appropriate.

React and the Frontend: Building Interactive Experiences React and the Frontend: Building Interactive Experiences Apr 11, 2025 am 12:02 AM

React is the preferred tool for building interactive front-end experiences. 1) React simplifies UI development through componentization and virtual DOM. 2) Components are divided into function components and class components. Function components are simpler and class components provide more life cycle methods. 3) The working principle of React relies on virtual DOM and reconciliation algorithm to improve performance. 4) State management uses useState or this.state, and life cycle methods such as componentDidMount are used for specific logic. 5) Basic usage includes creating components and managing state, and advanced usage involves custom hooks and performance optimization. 6) Common errors include improper status updates and performance issues, debugging skills include using ReactDevTools and Excellent

See all articles