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.
Note that the Parent's outputEvent method (that changes the Parent state) is invoked by the Child's button onClick event.
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.
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.
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.