1. How to update in batchesIn [Hooks], updating the status separately may cause multiple renderings of the page: import { useState } from 'react'; import { unstable_batchedUpdates } from 'react-dom'; //Use when updating status in batches import React from 'react'; const Example = () => { const [count, setCount] = useState(0); const [count1, setCount1] = useState(0); const [isClick, setCount2] = useState(0); setTimeout(function () { setCount(1) setCount1(1) setCount2(1) }, 1000); console.log('rendered') return ( <span>Please check the console output! </span> ); } export default Example; Console Output
So you need to use batch updates to avoid this problem! This is implemented in import { useState } from 'react'; import { unstable_batchedUpdates } from 'react-dom'; //Use when updating status in batches import React from 'react'; const Example = () => { const [count, setCount] = useState(0); const [count1, setCount1] = useState(0); const [isClick, setCount2] = useState(0); setTimeout(function () { unstable_batchedUpdates(() => { setCount(1) setCount1(1) setCount2(1) }) // This is the event being processed}, 1000); console.log('rendered') return ( <span>Please check the console output! </span> ); } export default Example; Console Output
2. How Hooks Get Routing Parameters Sometimes we will specify parameters in <Route path="/test/:name" component={Statistics} /> In Class, you can get the parameters of the url through If it is Hooks, you can get it like this: import { useState } from 'react'; import React from 'react'; const Example = ({ match }) => { const [name] = useState(match.params.name); return ( <p>The name is: <span style={{ fontWeight: 600 }}>{name}</span></p> ); } export default Example;
Execution effect The above is the detailed content of the analysis of the example of batch updating state and obtaining routing parameters in ReactHooks. For more information about batch updating state and obtaining routing parameters in ReactHooks, please pay attention to other related articles on 123WORDPRESS.COM! You may also be interested in:
|
<<: Summary of three ways to create new elements
1. What is positioning? The position attribute in...
Table of contents Introduction Architecture Advan...
Question 1: How do you instruct the browser to dis...
prune To use this command, both the client and da...
The solutions to the problems encountered during x...
After creating a container locally, you can creat...
This article describes how to use the local yum s...
This article shares the specific code of Vue to a...
Table of contents 1. Brief Introduction 2. setInt...
Method 1: Modify the configuration file (need to ...
Preface After MySQL version 3.23.44, InnoDB engin...
1. Download MySQL 1.1 Download address https://do...
background As the business develops, the company&...
We all know that after the MySQL database is inst...
MySQL implements Oracle-like sequences Oracle gen...