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

12 Javascript table controls (DataGrid) are sorted out

When the DataSource property of a DataGrid control...

WeChat applet implements sorting function based on date and time

I recently took over a small program project, and...

CentOS 6.5 installation mysql5.7 tutorial

1. New Features MySQL 5.7 is an exciting mileston...

HTML table tag tutorial (12): border style attribute FRAME

Use the FRAME property to control the style type ...

Implementation of navigation bar and drop-down menu in CSS

1. CSS Navigation Bar (1) Function of the navigat...

The use of MySQL triggers and what to pay attention to

Table of contents About Triggers Use of triggers ...

Detailed explanation of Json format

Table of contents A JSON is built on two structur...

Use pure JS to achieve the secondary menu effect

This article example shares the specific code of ...

Solve the problem of using linuxdeployqt to package Qt programs in Ubuntu

I wrote some Qt interface programs, but found it ...

MySQL complete collapse: detailed explanation of query filter conditions

Overview In actual business scenario applications...

Sharing experience on MySQL slave maintenance

Preface: MySQL master-slave architecture should b...

Simple steps to implement H5 WeChat public account authorization

Preface Yesterday, there was a project that requi...