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 Start the Docker service First you need to know...
Searching online for methods to deploy Angular pr...
Prerequisite: Percona 5.6 version, transaction is...
character Decimal Character Number Entity Name --...
Summary: In order to make your web page look more...
What is ssh Administrators can log in remotely to...
EXPLAIN shows how MySQL uses indexes to process s...
Most people compile MySQL and put it in the syste...
question When I was writing a project function to...
1. Introduction Some time ago, there were a serie...
<Head>……</head> indicates the file he...
I have been making websites for a long time, but I...
Preface Everyone knows how to run a jar package o...
1. Create a Docker network docker network create ...
First, let's introduce several common operati...