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

Docker installs ClickHouse and initializes data testing

Clickhouse Introduction ClickHouse is a column-or...

How to create a test database with tens of millions of test data in MySQL

Sometimes you need to create some test data, base...

Detailed explanation of Docker Secret management and use

1. What is Docker Secret 1. Scenario display We k...

Summary of knowledge points about covering index in MySQL

If an index contains (or covers) the values ​​of ...

Start a local Kubernetes environment using kind and Docker

introduce Have you ever spent a whole day trying ...

Notes on upgrading to mysql-connector-java8.0.27

Recently, an online security scan found a vulnera...

Understanding the CSS transform-origin property

Preface I recently made a fireworks animation, wh...

Various front-end printing methods of web: CSS controls web page printing style

CSS controls the printing style of web pages : Use...

Complete list of CentOS7 firewall operation commands

Table of contents Install: 1. Basic use of firewa...

Docker container data volume named mount and anonymous mount issues

Table of contents What is a container data volume...

Solution to Nginx SSL certificate configuration error

1. Introduction When a web project is published o...

Example analysis of mysql shared lock and exclusive lock usage

This article uses examples to illustrate the usag...

How to set up ssh password-free login to Linux server

Every time you log in to the test server, you alw...

Detailed explanation of the use of Vue mixin

Table of contents Use of Vue mixin Data access in...