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 some text in component 1 and display it 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
//在父层中的constructor中定义状态为一个空的message,this.state = {message:''} class Parent extends React.Component{ constructor(...args){ super(...args); this.state = { message:'' }
and then in the parent The
<Show onShow={this.state.message}/>
##
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 content of the message in the parent layer state to make the content of the bound display layer change accordingly.
Promote the state (data) of the input layer to the parent component. Below It 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: there is no onInp() and content. ?Don’t worry, continue reading
Then rewrite the input layer subcomponent 1 in the parent 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}/>
); } }
##
// 父组 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 entire content of this article, I hope it will be helpful to everyone The learning is helpful, and I hope everyone will support Script Home.
The above is the detailed content of Examples of communication between React components. 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中没有双向绑定;react的设计思想就是单向数据流,没有双向绑定的概念;react是view层,单项数据流只能由父组件通过props将数据传递给子组件,满足了view层渲染的要求并且更易测试与控制,所以在react中没有双向绑定。


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

WebStorm Mac version
Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Chinese version
Chinese version, very easy to use

Dreamweaver Mac version
Visual web development tools