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

How to implement two-way binding function in vue.js with pure JS

Table of contents First, let's talk about the...

MySQL data table partitioning strategy and advantages and disadvantages analysis

Table of contents Why do we need partitions? Part...

Centos7 implements sample code for restoring data based on MySQL logs

Introduction Binlog logs, that is, binary log fil...

MySQL partitioning practice through Navicat

MySQL partitioning is helpful for managing very l...

Detailed steps to install Hadoop cluster under Linux

Table of contents 1. Create a Hadoop directory in...

30 free high-quality English ribbon fonts

30 free high-quality English ribbon fonts for down...

Simple use of Vue bus

Simple use of Vue bus Scenario description: Compo...

vue-cropper component realizes image cutting and uploading

This article shares the specific code of the vue-...

Detailed explanation of JavaScript to monitor route changes

Table of contents history pushState() Method push...

Mysql experiment: using explain to analyze the trend of indexes

Overview Indexing is a skill that must be mastere...

MySQL Interview Questions: How to Set Up Hash Indexes

In addition to B-Tree indexes, MySQL also provide...

Detailed tutorial on building a local idea activation server

Preface The blogger uses the idea IDE. Because th...