React implements multi-component value transfer function through conetxt

React implements multi-component value transfer function through conetxt

The effect of this function is similar to vue的provide/inject
React can be done through context

insert image description here

Define a public file context/Theme.jsx

import { createContext } from 'react';
const theme = createContext()
export default theme

The parent component imports public files and child components and passes theme值

import React, { useState } from 'react';
import Child from "@/components/Child.jsx"
import Theme from "@/context/Theme.jsx"
export default () => {
    const [theme, setTheme] = useState("blue")
    return (
        <>
            <button onClick={() => setTheme("green")}>Check if context is responsive</button>
            <Theme.Provider value={theme}>
                <Child />
            </Theme.Provider>
        </>
    )
}

Child component gets data components/Child.jsx

import React from 'react';
import Theme from "@/context/Theme.jsx"
export default () => {
    return (
        <Theme.Consumer>
            {data => <p>Receive the value passed by the parent component context: {data}</p>}
        </Theme.Consumer>
    );
}

This concludes this article about React’s multi-component value transfer through conetxt. For more information about React’s multi-component value transfer, please search 123WORDPRESS.COM’s previous articles or continue browsing the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Detailed explanation of the relationship between React component value passing
  • Three ways to pass values ​​in react components
  • Detailed explanation of value transfer between Vue and React components
  • React method of passing values ​​between parent and child components
  • React child component transfers value to parent component

<<:  CSS techniques for achieving multi-column equal height layout that the front end should master

>>:  How to make ApacheBench support multi-url

Recommend

How to view and optimize MySql indexes

MySQL supports hash and btree indexes. InnoDB and...

Summary of MySQL LOAD_FILE() function method

In MySQL, the LOAD_FILE() function reads a file a...

Analysis of the Principle of MySQL Index Length Limit

This article mainly introduces the analysis of th...

jQuery plugin to implement minesweeper game (1)

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

Summary of the use of TypeScript in React projects

Preface This article will focus on the use of Typ...

How to export and import .sql files under Linux command

This article describes how to export and import ....

10 Underused or Misunderstood HTML Tags

Here are 10 HTML tags that are underused or misun...

Pure CSS custom multi-line ellipsis problem (from principle to implementation)

How to display text overflow? What are your needs...

CSS naming conventions (rules) worth collecting Commonly used CSS naming rules

CSS naming conventions (rules) Commonly used CSS ...

In-depth understanding of Worker threads in Node.js

Table of contents Overview The history of CPU-bou...

Summary of Vue component basics

Component Basics 1 Component Reuse Components are...

MySQL 5.7.18 installation tutorial and problem summary

MySQL 5.7.18 installation and problem summary. I ...

Four categories of CSS selectors: basic, combination, attribute, pseudo-class

What is a selector? The role of the selector is t...