1. What isWe can split the communication between components into two words:
Looking back at the Vue series of articles, components are one of the most powerful features of Compared with Communication refers to the process by which a sender transmits information to a receiver in a certain format through a certain medium in order to achieve a certain purpose. In a broad sense, any information traffic is communication. Communication between components means that components transmit information in some way to achieve a certain purpose. 2. How to communicateThere are many ways to transfer components, which can be divided into the following according to the sender and receiver:
Passing from parent component to child component Since the data flow of When the parent component calls the child component, it only needs to pass parameters in the child component tag, and the child component can receive the parameters passed by the parent component through function EmailInput(props) { return ( <label> Email: <input value={props.email} /> </label> ); } const element = <EmailInput email="[email protected]" />; Child component passes to parent component The basic idea of child component communication with parent component is that the parent component passes a function to the child component, and then gets the value passed by the child component through the callback of this function The corresponding code of the parent component is as follows: class Parents extends Component { constructor() { super(); this.state = { price: 0 }; } getItemPrice(e) { this.setState({ price: }); } render() { return ( <div> <div>price: {this.state.price}</div> {/* Pass a function into the child component*/} <Child getPrice={this.getItemPrice.bind(this)} /> </div> ); } } The corresponding code of the subcomponent is as follows: class Child extends Component { clickGoods(e) { // Pass the value into this function this.props.getPrice(e); } render() { return ( <div> <button onClick={this.clickGoods.bind(this, 100)}>goods1</button> <button onClick={this.clickGoods.bind(this, 1000)}>goods2</button> </div> ); } } Communication between sibling components If the data is transferred between sibling components, the parent component acts as an intermediate layer to achieve data intercommunication. class Parent extends React.Component { constructor(props) { super(props) this.state = {count: 0} } setCount = () => { this.setState({count: this.state.count + 1}) } render() { return ( <div> <SiblingA count = {this.state.count} /> <SiblingB onClick={this.setCount} /> </div> ); } } Passing from parent component to descendant component It is a common thing for a parent component to pass data to its descendant components, just like global data. Using Create a const PriceContext = React.createContext('price') After
If you want to get the data passed by class MyClass extends React.Component { static contextType = PriceContext; render() { let price = this.context; /* Perform rendering based on this value*/ } } Consumer component: <PriceContext.Consumer> { /* This is a function */ } { price => <div>price:{price}</div> } </PriceContext.Consumer> Non-relational component transfer If the relationship between components is complex, it is recommended to manage the data as a global resource to achieve communication, such as Conclusion Since Therefore, it can be seen that during the communication process, the storage location of the data is stored in the parent location This concludes this article about how components communicate in React. For more information about communication between React components, please search previous articles on 123WORDPRESS.COM or continue browsing the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: MySQL 8.0.17 installation graphic tutorial
>>: Linux file/directory permissions and ownership management
This article mainly introduces how to implement a...
1. Introduction to Layer 4 Load Balancing What is...
Table of contents 1. Create a Hadoop directory in...
Update: Now you can go to the MySQL official webs...
Implementation effect: 1. count(1) and count(*) W...
This article example shares the specific code of ...
This article uses examples to illustrate the comm...
Preface If you use the overflow: scroll attribute...
Generally, click events will be divided into diff...
1. Necessity of Tuning I have always been reluct...
Table of contents Project Background start Create...
Similar structures: Copy code The code is as foll...
About password strength verification: [root@mysql...
Table of contents Overview 1. Acquisition and pro...
Before the arrow was shot, the bow whispered to t...