What are the methods of react communication?
React communication methods include: 1. Use props for parent-child component communication; 2. Use callback functions for child-parent component communication; 3. Use variable promotion for sibling component communication; 4. Use Context for cross-component communication; 5. Use node’s events module for single instance communication; 6. Use redux to share data communication.
The operating environment of this tutorial: Windows7 system, react17.0.1 version, Dell G3 computer.
Six communication methods of React
1. Props parent-child communication
2. Callback function, child-father communication
3 .Variable promotion, sibling component communication
4.Context, cross-component communication
5.Single instance communication of node’s events module
6.redux shared data communication
1.propsFather-child communication
function Children(props) { return ( <div> <p>Children</p> <p>{props.text}</p> </div> ) } function Parent() { return ( <div> <p>Parent</p> <Children text="这是爸爸传给你的东西"></Children> </div> ) } export default Parent
2.Callback function, child-parent communication
function Children(props) { return ( <div> <p>Children</p> <p>{props.text}</p> <button onClick={() => { props.handleChange('改变了') }}> 点击我改变爸爸传给我的东西 </button> </div> ) } function Parent() { let [text, setText] = useState('这是爸爸传给你的东西') function handleChange(val) { setText(val) } return ( <div> <p>Parent</p> <Children handleChange={handleChange} text={text}></Children> </div> ) } export default Parent
3. Variable promotion, communication among brothers
function Children(props) { return ( <div> <p>Children</p> <button onClick={() => { props.setText('我是Children发的信息') }}>给Children1发信息</button> </div> ) } function Children1(props) { return ( <div> <p>Children1</p> <p>{props.text}</p> </div> ) } function App() { let [text, setText] = useState('') return ( <> <div>APP</div> <Children setText={setText}></Children> <Children1 text={text}></Children1> </> ) } export default App
4.Context, communication across groups
Old way of writing
class Children extends React.Component { static contextTypes = { text: PropsType.string } render() { return ( <div> <p>Children</p> <p>{this.context.text}</p> </div> ) } } class Parent extends React.Component { static childContextTypes = { text: PropsType.string } getChildContext() { return { text: '我是爸爸的信息' } } render() { return ( <div> <p>Parent</p> <Children></Children> </div> ) } } export default Parent
New way of writing
const { Consumer, Provider } = React.createContext() class Children extends React.Component { render() { return ( <Consumer> { (value) => ( <div> <p>Children1</p> <p>{value.text}</p> </div> ) } </Consumer> ) } } class Parent extends React.Component { render() { return ( <Provider value={{ text: '我是context' }}> <div> <p>Parent</p> <Children1></Children1> </div> </Provider> ) } } export default Parent
5.Single instance communication of the events module of node
function Children(props) { return ( <div> <p>Children</p> <p>{props.text}</p> <button onClick={() => { props.event.emit('foo') }}>点击我改变爸爸传给我的东西</button> </div> ) } function Parent() { let [text, setText] = useState('这是爸爸传给你的东西') let event = new Events() event.on('foo', () => { setText('改变了') }) return ( <div> <p>Parent</p> <Children event={event} text={text}></Children> </div> ) } export default Parent
Note⚠️: For this kind of communication, remember to introduce the events module at the top, no need to install, node its own module.
6.redux shared data communication
store.js
import { createStore } from 'redux' let defaultState = { text: '我是store' } let reducer = (state = defaultState, action) => { switch (action) { default: return state } } export default createStore(reducer)
child.js
import React from 'react' import { connect } from 'react-redux' class Child extends React.Component { render() { return ( <div>Child<p>{this.props.text}</p></div> ) } } let mapStataToProps = (state) => { return { text: state.text } } export default connect(mapStataToProps, null)(Child)
Parent.js
class Parent extends React.Component { render() { return ( <Provider store={store}> <div> <p>Parent</p> <Child></Child> </div> </Provider> ) } } export default Parent
Note: Remember to install redux and react-redux.
[Related recommendations: Redis video tutorial]
The above is the detailed content of What are the methods of react communication?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



React front-end and back-end separation guide: How to achieve front-end and back-end decoupling and independent deployment, specific code examples are required In today's web development environment, front-end and back-end separation has become a trend. By separating front-end and back-end code, development work can be made more flexible, efficient, and facilitate team collaboration. This article will introduce how to use React to achieve front-end and back-end separation, thereby achieving the goals of decoupling and independent deployment. First, we need to understand what front-end and back-end separation is. In the traditional web development model, the front-end and back-end are coupled

In the previous article (link), Xiao Zaojun introduced the development history of broadband technology from ISDN, xDSL to 10GPON. Today, let’s talk about the upcoming new generation of optical fiber broadband technology-50GPON. █F5G and F5G-A Before introducing 50GPON, let’s talk about F5G and F5G-A. In February 2020, ETSI (European Telecommunications Standards Institute) promoted a fixed communication network technology system based on 10GPON+FTTR, Wi-Fi6, 200G optical transmission/aggregation, OXC and other technologies, and named it F5G. That is, the fifth generation fixed network communication technology (The5thgenerationFixednetworks). F5G is a fixed network

How to build a reliable messaging application with React and RabbitMQ Introduction: Modern applications need to support reliable messaging to achieve features such as real-time updates and data synchronization. React is a popular JavaScript library for building user interfaces, while RabbitMQ is a reliable messaging middleware. This article will introduce how to combine React and RabbitMQ to build a reliable messaging application, and provide specific code examples. RabbitMQ overview:

ReactRouter User Guide: How to Implement Front-End Routing Control With the popularity of single-page applications, front-end routing has become an important part that cannot be ignored. As the most popular routing library in the React ecosystem, ReactRouter provides rich functions and easy-to-use APIs, making the implementation of front-end routing very simple and flexible. This article will introduce how to use ReactRouter and provide some specific code examples. To install ReactRouter first, we need

Original title: "How does a wireless mouse become wireless?" 》Wireless mice have gradually become a standard feature of today’s office computers. From now on, we no longer have to drag long cords around. But, how does a wireless mouse work? Today we will learn about the development history of the No.1 wireless mouse. Did you know that the wireless mouse is now 40 years old? In 1984, Logitech developed the world's first wireless mouse, but this wireless mouse used infrared as a The signal carrier is said to look like the picture below, but later failed due to performance reasons. It was not until ten years later in 1994 that Logitech finally successfully developed a wireless mouse that works at 27MHz. This 27MHz frequency also became the wireless mouse for a long time.

How to use React and Apache Kafka to build real-time data processing applications Introduction: With the rise of big data and real-time data processing, building real-time data processing applications has become the pursuit of many developers. The combination of React, a popular front-end framework, and Apache Kafka, a high-performance distributed messaging system, can help us build real-time data processing applications. This article will introduce how to use React and Apache Kafka to build real-time data processing applications, and

PHP, Vue and React: How to choose the most suitable front-end framework? With the continuous development of Internet technology, front-end frameworks play a vital role in Web development. PHP, Vue and React are three representative front-end frameworks, each with its own unique characteristics and advantages. When choosing which front-end framework to use, developers need to make an informed decision based on project needs, team skills, and personal preferences. This article will compare the characteristics and uses of the three front-end frameworks PHP, Vue and React.

Integration of Java framework and React framework: Steps: Set up the back-end Java framework. Create project structure. Configure build tools. Create React applications. Write REST API endpoints. Configure the communication mechanism. Practical case (SpringBoot+React): Java code: Define RESTfulAPI controller. React code: Get and display the data returned by the API.
