MOCKSTACKS
EN
Questions And Answers

More Tutorials









React Communication Between Components


Sending data back to the parent, to do this we simply pass a function as a prop from the parent component to the child component, and the child component calls that function.

In this example, we will change the Parent state by passing a function to the Child component and invoking that function inside the Child component.

import React from 'react';
class Parent extends React.Component {
 constructor(props) {
 super(props);
 this.state = { count: 0 };
 this.outputEvent = this.outputEvent.bind(this);
 }
 outputEvent(event) {
 // the event context comes from the Child
 this.setState({ count: this.state.count++ });
 }
 render() {
 const variable = 5;
 return (
 <div>
 Count: { this.state.count }
 <Child clickHandler={this.outputEvent} />
 </div>
 );
 }
}
class Child extends React.Component {
 render() {
 return (
 <button onClick={this.props.clickHandler}>
 Add One More
 </button>
 );
 }
}
export default Parent;

Note that the Parent's outputEvent method (that changes the Parent state) is invoked by the Child's button onClick event.

Not-related Components


The only way if your components does not have a parent-child relationship (or are related but too further such as a grand grand grand son) is to have some kind of a signal that one component subscribes to, and the other writes into.

Those are the 2 basic operations of any event system: subscribe/listen to an event to be notify, and send/trigger/publish/dispatch a event to notify the ones who wants.

There are at least 3 patterns to do that. You can find a comparison here.

Here is a brief summary:

Pattern 1: Event Emitter/Target/Dispatcher: the listeners need to reference the source to subscribe.

to subscribe: otherObject.addEventListener('click', () => { alert('click!'); });
to dispatch: this.dispatchEvent('click');


Pattern 2: Publish/Subscribe: you don't need a specific reference to the source that triggers the event, there is a global object accessible everywhere that handles all the events.

to subscribe: globalBroadcaster.subscribe('click', () => { alert('click!'); });
to dispatch: globalBroadcaster.publish('click');


Pattern 3: Signals: similar to Event Emitter/Target/Dispatcher but you don't use any random strings here. Each object that could emit events needs to have a specific property with that name. This way, you know exactly what events can an object emit.

to subscribe: otherObject.clicked.add( () => { alert('click'); });
to dispatch: this.clicked.dispatch();


Conclusion

In this page (written and validated by ) you learned about React Communication Between Components . What's Next? If you are interested in completing React tutorial, your next topic will be learning about: React Stateless Functional Components.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.