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

CSS positioning layout (position, positioning layout skills)

1. What is positioning? The position attribute in...

Detailed steps for installing and using vmware esxi6.5

Table of contents Introduction Architecture Advan...

Docker volume deletion operation

prune To use this command, both the client and da...

Solutions to the Problem of Creating XHTML and CSS Web Pages

The solutions to the problems encountered during x...

CentOS uses local yum source to build LAMP environment graphic tutorial

This article describes how to use the local yum s...

Vue implements the function of calling the mobile phone camera and album

This article shares the specific code of Vue to a...

JavaScript Timer Details

Table of contents 1. Brief Introduction 2. setInt...

Docker - Summary of 3 ways to modify container mount directories

Method 1: Modify the configuration file (need to ...

Creation, constraints and deletion of foreign keys in MySQL

Preface After MySQL version 3.23.44, InnoDB engin...

Detailed process of building mysql5.7.29 on centos7 of linux

1. Download MySQL 1.1 Download address https://do...

3 ways to correctly modify the maximum number of connections in MySQL

We all know that after the MySQL database is inst...

MySQL implements a solution similar to Oracle sequence

MySQL implements Oracle-like sequences Oracle gen...