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
The following functions are implemented: 1. Usern...
--1. Create a new group and user for mysql # user...
Introduction: MySQL database recovery by time poi...
Table of contents What is a slot Understanding of...
introduction It is okay to add or not add a semic...
/***************** * proc file system************...
Standalone hbase, let’s talk about it first. Inst...
This article shares the specific code for WeChat ...
This article records the process of upgrading MyS...
Table of contents Creating OAuth Apps Get the cod...
Table of contents 1. Vue initialization vue entry...
grammar background: linear-gradient(direction,col...
Table of contents 1. General steps for SQL optimi...
1. The catalina.bat must be set to UTF-8. If I do...
Related articles: Beginners learn some HTML tags ...