Detailed explanation of how to pass values ​​between react hooks components (using ts)

Detailed explanation of how to pass values ​​between react hooks components (using ts)

From father to son

Pass values ​​through props and use useState to control the state value

In the parent component Father.tsx:

Parent Component

In the child component Child.tsx:

Subcomponents

Display effect:

Display effect

From son to father

Just like react, the callback function is passed into the child component, and the state of the parent component is updated by receiving the return value of the child component.

Parent component, in Father.tsx:

Parent Component

Child component, in Child.tsx:

Subcomponents

Display effect:

Display effect

Optimized version of child-to-parent transmission, using useCallback to store functions that handle events

Parent component, in Father.tsx:

Parent Component

Child component, in Child.tsx:

Subcomponents

Cross-level components (parent to descendant)

Use useContext to pass values, similar to React's Context

Steps:

Create a context and use context.provider to associate the component that needs to pass the value to introduce context, and useContext to get the value

Parent component, in Father.tsx:

Parent Component

Child component, in Child.tsx:

Subcomponents

Grandchild component, in Sun.tsx:

Grandchild Component

Display effect

Display effect

This concludes this article on how to pass values ​​between react hooks components (using ts). For more information about passing values ​​between react hooks components, please search previous articles on 123WORDPRESS.COM or continue browsing the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • ReactHooks batch update state and get route parameters example analysis
  • React Hooks Detailed Explanation
  • Common pitfalls of using React Hooks
  • 30 minutes to give you a comprehensive understanding of React Hooks
  • How React Hooks Work
  • Common usage of hook in react
  • Introduction to 10 Hooks in React

<<:  Implementation of postcss-pxtorem mobile adaptation

>>:  Three ways of html+css layout (natural layout/flow layout/positioning layout)

Recommend

What you need to understand about MySQL locks

1. Introduction MySQL locks can be divided into g...

Two ways to achieve horizontal arrangement of ul and li using CSS

Because li is a block-level element and occupies ...

How to set Nginx to forward the domain name to the specified port

Enter /usr/local/nginx/conf sudo cd /usr/local/ng...

Summary of how to add root permissions to users in Linux

1. Add a user . First, use the adduser command to...

Basic notes on html and css (must read for front-end)

When I first came into contact with HTML, I alway...

JavaScript programming through Matlab centroid algorithm positioning learning

Table of contents Matlab Centroid Algorithm As a ...

Several reasons for not compressing HTML

The reason is simple: In HTML documents, multiple ...

JS implements the snake game

Table of contents 1. Initialization structure 2. ...

Mini Program to Implement Text Circular Scrolling Animation

This article shares the specific code of the appl...

The visual design path of the website should conform to user habits

Cooper talked about the user's visual path, w...

Analysis of the principle of Mybatis mapper dynamic proxy

Preface Before we start explaining the principle ...

A brief discussion on MySQL large table optimization solution

background The amount of new data in the business...

Implementation of building custom images with Dockerfile

Table of contents Preface Introduction to Dockerf...

Solution for Tomcat to place configuration files externally

question When we are developing normally, if we w...