Parent component communicates with child components
Define ref reference in parent group import React,{Component,createRef} from 'react' import Child1 from './Child1' export default class App extends Component { constructor(props){ super(props) this.child = createRef() } render(){ return( <div> <Child1 ref={this.child}/> <button onClick={this.fn.bind(this)}></button> </div> ) } fn(){ const child = this.child.current child.setTitle() } } Subcomponent defines the data source and how to modify the data source import React,{Component} from 'react' export default class Child1 extends Component{ state={ title:'Title' } render(){ return ( <div> {this.state.title} </div> ) } setTitle(){ this.setstate({title:'hh'}) } } Child components communicate with parent components The parent component passes one of its own methods to the child component. You can do anything in the method, such as changing the state. The child component receives the parent component's method through Cross-component communicationReact does not have an event bus like Vue to solve this problem. One way is to use their common parent component to implement it through proxy, but the process will be quite cumbersome. React provides Context to achieve cross-component communication without having to explicitly pass props through each layer of the component tree.
Ancestors and descendants
import React , {createContext} from 'react' let {Provider, Consumer} = createContext() export { Provider, //Publish Consumer //Subscribe} 2. Ancestor Node import React ,{Component} from 'react' import {Provider,Consumer} from './store' import Son from './Son' export default class App extends Component { constructor(props){ super(props) this.state={ name:'mingcen' } } render(){ return ( <div> <Provider value={this.state.name}> <Son/> </Provider> </div> ) } } 3. Descendant nodes import React,{Component} from'react' import {Consumer} from './store' export default class Son1 extends Component{ constructor(props){ super(props) this.state={ name:'uuu' } } render(){ return( <div> <Consumer> { value=>{ <div>{value.name}</div> } } </Consumer> </div> ) } } Brother node communication
ancestor state={ count:1, setCount:()=>{ this.setState(state=>{ return { count:++state.count } }) } } render(){ let {count,setCount} = this.state return( <div> <Provider value={{count,setCount}}> <Cmp1></Cmp1> <Cmp2></Cmp2> </Provider> </div> ) } Brother Cmp2 import React, { Component , createContext } from 'react' export default class Cmp2 extends Component { // Only got the default data --> not wrapped in the Provider component static contextType = createContext render() { return ( <div> <button onClick={this.setCount.bind(this)}>Auto-increment data</button> </div> ) } setCount() { this.context.setCount() } } Brother Cmp1 <Consumer> { value => <h3>{value.count}</h3> } </Consumer> SummarizeThis article ends here. I hope it can be helpful to you. I also hope that you can pay more attention to more content on 123WORDPRESS.COM! You may also be interested in:
|
<<: Markup language - specify CSS styles for text
>>: Analysis of the usage of Xmeter API interface testing tool
It is very common to see images and text displaye...
Preface: As a junior programmer, I dream of build...
Disadvantages of single-node database Large-scale...
In this post, we’ll use the :placeholder-shown ps...
System performance expert Brendan D. Gregg update...
Table of contents 1. Customize the search bar con...
When MySQL performs DDL operations such as alter ...
This article example shares the specific code of ...
The EXPLAIN statement is introduced in MySQL quer...
First method Alibaba Cloud and Baidu Cloud server...
a href="#"> After clicking the link, ...
The default ssh port number of Linux servers is g...
The floating-point types supported in MySQL are F...
Table of contents 1. Basic event handling 2. Send...
Table of contents Preface Six features of JSON.st...