This time I will bring you a detailed explanation of the use of component communication in React. What are the precautions when using component communication in React? Here are practical cases, let’s take a look.
Component communication
Here we only talk about the communication between the React component and the component itself. Component communication is mainly divided into three parts:
Parent component to Subcomponent communication: The parent component passes parameters to the subcomponent or the parent component calls the method of the subcomponent
The subcomponent communicates with the parent component: The subcomponent passes parameters to the parent component or the child component Methods of calling parent components
Communication between sibling components: passing parameters or calling each other between sibling components
It is recommended not to have too deep embedding Set of relationships
The parent component communicates with the child component
Parent: The method of calling the child component is mainly used
this.refs.c1.changeChildren1
Parent: Passing parameters to child components mainly uses
text={this.state.text}
Child: Definition method changeChildren1 is used by the parent component to call the
child: use the property
this.props.text
to get the parameters passed from the parent component
//父组件向子组件通信 //父组件 var ParentComponent1 = React.createClass({ getInitialState: function(){ return { text: '' } }, //输入事件 change: function(event){ this.setState({text: event.target.value}); //调用子组件的方法 this.refs.c1.changeChildren1(event.target.value); }, render: function(){ return ( <p> </p><p><label>父组件</label><input></p> <childrencomponent1></childrencomponent1> ) } }) //子组件 var ChildrenComponent1 = React.createClass({ getInitialState: function(){ return { text: '' } }, //被父组件调用执行 changeChildren1: function(text){ this.setState({text: text}); }, render: function(){ return ( <p> </p><p>子组件-来自父组件的调用:{this.state.text}</p> <p>子组件-来自父组件的传参:{this.props.text}</p> ) } }) ReactDOM.render(<parentcomponent1></parentcomponent1>, document.getElementById('p1'));
The child component communicates with the parent component
Parent: Define the method changeParent for the child component to call
Child: The main method of calling the parent component Use
this.props.change(event.target.value);
##
//子组件向父组件通信 //父组件 var ParentComponent2 = React.createClass({ getInitialState: function(){ return { text: '' } }, //被子组件调用执行 changeParent: function(text){ this.setState({text: text}); }, render: function(){ return ( <p> </p><p>父组件-来自子组件的调用:{this.state.text}</p> <childrencomponent2></childrencomponent2> ) } }) //子组件 var ChildrenComponent2 = React.createClass({ getInitialState: function(){ return { text: '' } }, //输入事件 change: function(event){ //调用子组件的方法 this.props.change(event.target.value); }, render: function(){ return ( <p> </p><p><label>子组件</label><input></p> ) } }) ReactDOM.render(<parentcomponent2></parentcomponent2>, document.getElementById('p2'));Sibling component communication
- Method 1 : Communicate through a common parent component
//兄弟组间通信-通过共同的父组件通信 //父组件 var ParentComponent3 = React.createClass({ getInitialState: function(){ return { text: '' } }, //被子组件2调用,向子组件1通信 changeChildren1: function(text){ //调用子组件1的方法 this.refs.cp1.changeState(text); }, //被子组件1调用,向子组件2通信 changeChildren2: function(text){ //调用子组件2的方法 this.refs.cp2.changeState(text); }, render: function(){ return ( <p> </p><p>父组件-来自子组件的调用:{this.state.text}</p> <childrencomponent3_1></childrencomponent3_1> <childrencomponent3_2></childrencomponent3_2> ) } }) //子组件1 var ChildrenComponent3_1 = React.createClass({ getInitialState: function(){ return { text: '' } }, changeState: function(text){ this.setState({text: text}); }, //输入事件 change: function(event){ //调用子组件的方法 this.props.change(event.target.value); }, render: function(){ return ( <p> </p><p><label>子组件1</label><input></p> <p>来自子组件2的调用-{this.state.text}</p> ) } }) //子组件2 var ChildrenComponent3_2 = React.createClass({ getInitialState: function(){ return { text: '' } }, changeState: function(text){ this.setState({text: text}); }, //输入事件 change: function(event){ //调用子组件的方法 this.props.change(event.target.value); }, render: function(){ return ( <p> </p><p><label>子组件2</label><input></p> <p>来自子组件1的调用-{this.state.text}</p> ) } }) ReactDOM.render(<parentcomponent3></parentcomponent3>, document.getElementById('p3'));Method 2: Communication through context
and through The common parent component communicates the same, the difference is that the context
//兄弟组间通信-通过 context 通信 //父组件 var ParentComponent4 = React.createClass({ getChildContext: function(){ return { changeChildren1: function(text){ this.refs.cp1.changeState(text) }.bind(this), changeChildren2: function(text){ this.refs.cp2.changeState(text) }.bind(this) } }, childContextTypes: { changeChildren1: React.PropTypes.func.isRequired, changeChildren2: React.PropTypes.func.isRequired }, render: function(){ return ( <p> <childrencomponent4_1></childrencomponent4_1> <childrencomponent4_2></childrencomponent4_2> </p> ) } }) //子组件1 var ChildrenComponent4_1 = React.createClass({ getInitialState: function(){ return { text: '' } }, contextTypes: { changeChildren2: React.PropTypes.func.isRequired }, changeState: function(text){ this.setState({text: text}); }, //输入事件 change: function(event){ //调用子组件的方法 this.context.changeChildren2(event.target.value); }, render: function(){ return ( <p> </p><p><label>子组件1</label><input></p> <p>来自子组件2的调用-{this.state.text}</p> ) } }) //子组件2 var ChildrenComponent4_2 = React.createClass({ getInitialState: function(){ return { text: '' } }, contextTypes: { changeChildren1: React.PropTypes.func.isRequired }, changeState: function(text){ this.setState({text: text}); }, //输入事件 change: function(event){ //调用子组件的方法 this.context.changeChildren1(event.target.value); }, render: function(){ return ( <p> </p><p><label>子组件2</label><input></p> <p>来自子组件1的调用-{this.state.text}</p> ) } }); ReactDOM.render(<parentcomponent4></parentcomponent4>, document.getElementById('p4'));is called. I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the PHP Chinese website! Recommended reading:
Detailed explanation of the implementation steps of PromiseA
Detailed explanation of the steps to highlight the selected li in react implementation
The above is the detailed content of Detailed explanation of the use of component communication in React. For more information, please follow other related articles on the PHP Chinese website!

在react中,canvas用于绘制各种图表、动画等;可以利用“react-konva”插件使用canvas,该插件是一个canvas第三方库,用于使用React操作canvas绘制复杂的画布图形,并提供了元素的事件机制和拖放操作的支持。

在react中,antd是基于Ant Design的React UI组件库,主要用于研发企业级中后台产品;dva是一个基于redux和“redux-saga”的数据流方案,内置了“react-router”和fetch,可理解为应用框架。

React不是双向数据流,而是单向数据流。单向数据流是指数据在某个节点被改动后,只会影响一个方向上的其他节点;React中的表现就是数据主要通过props从父节点传递到子节点,若父级的某个props改变了,React会重渲染所有子节点。

因为在react中需要利用到webpack,而webpack依赖nodejs;webpack是一个模块打包机,在执行打包压缩的时候是依赖nodejs的,没有nodejs就不能使用webpack,所以react需要使用nodejs。

react是组件化开发;组件化是React的核心思想,可以开发出一个个独立可复用的小组件来构造应用,任何的应用都会被抽象成一颗组件树,组件化开发也就是将一个页面拆分成一个个小的功能模块,每个功能完成自己这部分独立功能。

react和reactdom的区别是:ReactDom只做和浏览器或DOM相关的操作,例如“ReactDOM.findDOMNode()”操作;而react负责除浏览器和DOM以外的相关操作,ReactDom是React的一部分。

在react中,forceupdate()用于强制使组件跳过shouldComponentUpdate(),直接调用render(),可以触发组件的正常生命周期方法,语法为“component.forceUpdate(callback)”。

react与vue的虚拟dom没有区别;react和vue的虚拟dom都是用js对象来模拟真实DOM,用虚拟DOM的diff来最小化更新真实DOM,可以减小不必要的性能损耗,按颗粒度分为不同的类型比较同层级dom节点,进行增、删、移的操作。


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

Zend Studio 13.0.1
Powerful PHP integrated development environment

SublimeText3 English version
Recommended: Win version, supports code prompts!

Dreamweaver Mac version
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools