| Start |
|---|
| Child to Parent 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 |
| 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 (opens new window). Here is a brief summary:
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. |
| Parent to Child Components That the easiest case actually, very natural in the React world and the chances are - you are already using it. You can pass props down to child components. In this example Here, the In summary, you have a component (parent) that renders another one (child) and passes to it some props. RemarksThere are a total of 3 cases of communication between React components:
|
Read Article
https://develop.horje.com/learn/1434/reference