Home > Article > Web Front-end > Example tutorial on communication between React components
This article mainly introduces the example code of communication between React components. The editor thinks it is quite good, so I will share it with you now and give it as a reference. Let’s follow the editor and take a look.
Recently, I have just started to learn the UI framework of react.js. The biggest feeling that the react library gives me is that it can completely take over the UI layer. When you want to change something in the view, you only need to change the state in this.state. I still like that as long as the things in the data layerView layer are manipulated, they will change. You can get rid of the direct operation of the DOM. After all, it would be more complicated to do it directly. It should be string mixed with various css in the logic layer js, which is a bit uncomfortable for me (this tag is also mixed in JSX , but I think it should not be regarded as a label, but as a statement (you will get used to it).
Go back to the focus of the past few days and talk about state transfer between react components.
Above code:
1. Define two sub-components child-1 and child-2
//child-1 子组件-1为输入框 class Input extends React.Component{ constructor(...args){ super(...args); } render(){ return <input type="text"/> } } //child-2 子组-2为显示框 class Show extends React.Component{ constructor(...args){ super(...args); } render(){ return <p></p> } }
2. Define the parent component Parent and insert the two child components into the parent component
class Parent extends React.Component{ constructor(...args){ super(...args); } render(){ return( <p> <Input}/> <Show/> </p> ); } }
The current task is to input the total in component 1 Some text is displayed in component 2 at the same time.
Analysis: To synchronize component 2 with component 1, let both components 1 and 2 bind the state of the parent component. That means keeping both components under control. The direction of data is that component 1 promotes its own data to the parent layer and saves it in the state of the parent layer. The data in the parent layer is passed to component 2 through propsproperty in component 2 and bound in the view layer.
The first step is to bind the 9304e6782376319895a69f5eee4dd6e2 component
//在父层中的constructor中定义状态为一个空的message,this.state = {message:''} class Parent extends React.Component{ constructor(...args){ super(...args); this.state = { message:'' }
Then9304e6782376319895a69f5eee4dd6e2 in the parent component Change to
<Show onShow={this.state.message}/>
Then we enter the 9304e6782376319895a69f5eee4dd6e2 component and bind the onShow attribute of this component to its content. 9304e6782376319895a69f5eee4dd6e2The component becomes
class Show extends React.Component{ constructor(...args){ super(...args); } render(){ return <p>{this.props.onShow}</p> }
In this way, the data of the display layer of component 2 has been bound. Next, we only need to change the status of the parent layer. The content of the message can make the content of the bound display layer change accordingly
Promote the status (data) of the input layer to the parent component. The following is the rewritten component 1
class Input extends React.Component{ constructor(...args){ super(...args); } //将输入的内容更新到自身组件的状态中,并且将改变后的状态作为参数传递给该组件的一个自定义属性onInp() fn(ev){ this.props.onInp(ev.target.value); } render(){ //用onInput(注意react中采用驼峰写法和原生的略有不同)绑定fn()函数 return <input type="text" onInput={this.fn.bind(this)} value={this.props.content}/> } }
There may be a problem when you see this: onInp() and content are not there? Don’t worry, continue reading
then rewrite the parent Input layer sub-component 1 in the component,
class Parent extends React.Component{ constructor(...args){ super(...args); this.state = { message:'' }; } //传进的text是其提升上来的状态,然后再更新父组件的状态 fn(text){ this.setState({ message:text }) } render(){ return(/* onInp就出现在这里,并绑定一个函数, 并且有一个content将父组件的状态同步到子组件中 */ <Show onShow={this.state.message}/>
); } }
The finished code is like this
// 父组 class Parent extends React.Component{ constructor(...args){ super(...args); this.state = { message:'' }; } onInp(text){ this.setState({ message:text }) } render(){ return(<Show onShow={this.state.message}/>
); } } //child-1 class Input extends React.Component{ constructor(...args){ super(...args); } fn(ev){ this.props.onInp(ev.target.value); } render(){ return } } //child-2 class Show extends React.Component{ constructor(...args){ super(...args); } render(){ return <p>{this.props.onShow}</p> } } //最后渲染出 ReactDOM.render(, document.getElementById('app') );
The above is the detailed content of Example tutorial on communication between React components. For more information, please follow other related articles on the PHP Chinese website!