ReactHooks batch update state and get route parameters example analysis

ReactHooks batch update state and get route parameters example analysis

1. How to update in batches

In [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

Rendered Rendered Rendered Rendered Rendered

So you need to use batch updates to avoid this problem!

This is implemented in class through setState

hooks can be implemented through unstable_batchedUpdates

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

Rendered Rendered

2. How Hooks Get Routing Parameters

Sometimes we will specify parameters in route , so that we can pass the component parameters directly through the URL

<Route path="/test/:name" component={Statistics} />

In Class, you can get the parameters of the url through this.props.match.params

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;

match.params is the parameters in the route

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:
  • React Hook: How to use Effect Hook
  • Understand react-hooks and example code in three minutes
  • Introduction to 10 Hooks in React
  • Teach you to create custom hooks in react
  • React Hooks Detailed Explanation
  • React Hook: How to use State Hook

<<:  Summary of three ways to create new elements

>>:  Implementation of nginx multiple locations forwarding any request or accessing static resource files

Recommend

Mysql database recovery actual record by time point

Introduction: MySQL database recovery by time poi...

Understanding and example code of Vue default slot

Table of contents What is a slot Understanding of...

About VSCode formatting JS automatically adding or removing semicolons

introduction It is okay to add or not add a semic...

Linux kernel device driver proc file system notes

/***************** * proc file system************...

How to deploy hbase using docker

Standalone hbase, let’s talk about it first. Inst...

WeChat applet realizes taking photos and selecting pictures from albums

This article shares the specific code for WeChat ...

Upgrade MySQL 5.1 to 5.5.36 in CentOS

This article records the process of upgrading MyS...

Example of implementing GitHub's third-party authorization method in Vue

Table of contents Creating OAuth Apps Get the cod...

A brief discussion on the whole process of Vue's first rendering

Table of contents 1. Vue initialization vue entry...

CSS container background 10 color gradient Demo (linear-gradient())

grammar background: linear-gradient(direction,col...

MYSQL's 10 classic optimization cases and scenarios

Table of contents 1. General steps for SQL optimi...

HTML Tutorial: Collection of commonly used HTML tags (6)

Related articles: Beginners learn some HTML tags ...