Solution to React pure function component setState not refreshing the page update

Solution to React pure function component setState not refreshing the page update

Problem description:

const [textList, setTextList] = useState(original array);
setTextList(new array);


When modifying the original array, if the original array is a deep array (more than one layer), using setTextList to modify it will not trigger a page refresh

Cause Analysis:

This involves the knowledge of mutable objects and immutable objects. In Vue and React, if you update a mutable object, it may cause the view to update. This is because Vue and React are shallow listeners by default, and only listen to the first layer of data. Changes in the inner layer data will not be monitored.

Solution:

My solution here is to first make a deep copy of the original array, assign it to the new array, then modify the new array and pass the modified new array in, which will cause the view to update.

var lists = textList.concat();
lists.splice(index, 1);
setTextList(lists);

Supplement: In react, useState updates do not render components when hooks are used

When using react and writing components like the one shown in the figure, I found a critical problem. When I choose to write it with class, it is easy to update the rendering through the component.
When I decided to refactor my components using functional component hooks, I found a very difficult problem. When I used onChange to change the value of the parent component, the value was changed, but the component was not re-rendered? ? ? ?
I was confused, so I tried to set the value to empty and then assign it to the class component. I failed, so I tried to look through the life cycle of the hook. . . . ------If it fails, try Baidu---I found the same problem. . . I found that I only needed to add slice() at the end.
So in order to solve the problem first, I changed the red circle in the figure to onChange(value.slice())
-----So, this magical problem was solved.

Now that the problem has been solved, let's go back and see what happened. . .
Looking at the document, I found a sentence that the data in useState must be immutable (non-assignable object)
That is, the state of the ass component also advocates the use of immutable data, but it is not mandatory, because as long as setState is called, it will trigger an update. Therefore, this problem does not occur in the class component, or the update can be triggered by changing it to empty and then assigning a value.
However, when using useState again, if the same object is passed into the update function, the update will not be triggered.
So the solution is to return a new object through slice() to assign the value, which is the key to solving the problem. . . .

The above is the detailed solution to the problem that the setState update page of react pure function component does not refresh. For more information about the react useState page not refreshing, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • Detailed explanation of using React.memo() to optimize the performance of function components
  • Do you know the difference between React functional components and class components?
  • Comparison of the use and advantages of React function components and class components

<<:  VMware vsphere 6.5 installation tutorial (picture and text)

>>:  Alibaba Cloud ECS cloud server (linux system) cannot connect remotely after installing MySQL (pitfall)

Recommend

HTML basic syntax is convenient for those who are just starting to learn HTML

1.1 General marking A general tag consists of an ...

How to install nginx in centos7

Install the required environment 1. gcc installat...

Vue3.0 implements encapsulation of checkbox components

This article example shares the specific code of ...

Docker image management common operation code examples

Mirroring is also one of the core components of D...

Correct use of Vue function anti-shake and throttling

Preface 1. Debounce: After a high-frequency event...

Vue uses echarts to draw an organizational chart

Yesterday, I wrote a blog about the circular prog...

Detailed explanation of built-in methods of javascript array

Table of contents 1. Array.at() 2. Array.copyWith...

Website Design Experience Summary of Common Mistakes in Website Construction

Reminder: Whether it is planning, designing, or de...

Implementing a simple timer in JavaScript

This article example shares the specific code of ...

jQuery implements breathing carousel

This article shares the specific code of jQuery t...

Building a KVM virtualization platform on CentOS7 (three ways)

KVM stands for Kernel-based Virtual Machine, whic...

How to use jsx syntax correctly in vue

Table of contents Preface Virtual DOM What is Vir...

JavaScript to achieve dynamic color change of table

This article shares the specific code for JavaScr...