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
background All of the company's servers are p...
HTML onfocus Event Attributes Definition and Usag...
I've seen people asking before, how to add sty...
Preface The author has always felt that it would ...
The docker image id is unique and can physically ...
Basic Concepts Current read and snapshot read In ...
1. Server setup The remote repository is actually...
Back in the Kernel 2.6 era, a new security system...
I recently upgraded MySQL to 5.7, and WordPress r...
Table of contents Stabilization Throttling: Anti-...
<br />Sometimes you may be asked questions l...
type is the control used for input and output in t...
1. Problem There is a table as shown below, we ne...
7 ways to implement a two-column layout with fixe...
MySQL paging queries are usually implemented thro...