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

jQuery treeview tree structure application

This article example shares the application code ...

CSS3 uses animation attributes to achieve cool effects (recommended)

animation-name animation name, can have multiple ...

A brief discussion on the role of HTML empty links

Empty link: That is, there is no link with a targ...

How to install and configure the Docker Compose orchestration tool in Docker.v19

1. Introduction to Compose Compose is a tool for ...

Detailed explanation of Nginx timed log cutting

Preface By default, Nginx logs are written to a f...

CSS code to control the background color of the web page

I think everyone often worries about finding pict...

Summary of methods to clear cache in Linux system

1) Introduction to cache mechanism In the Linux s...

Detailed explanation of Docker working mode and principle

As shown in the following figure: When we use vir...

About the garbled problem caused by HTML encoding

Today a junior student asked a question. The HTML...

Detailed explanation of the underlying encapsulation of Java connection to MySQL

This article shares the Java connection MySQL und...

Error mysql Table 'performance_schema...Solution

The test environment is set up with a mariadb 5.7...

Web page CSS priority is explained in detail for you

Before talking about CSS priority, we need to und...

How MySQL handles implicit default values

Some students said that they encountered the prob...