How to be Notified of an Action between React Components?<!-- --> | <!-- -->Patrick Desjardins Blog
Patrick Desjardins Blog
Patrick Desjardins picture from a conference

How to be Notified of an Action between React Components?

Posted on: November 8, 2021

The topic of this article follows the recent topic of communication across React component. The following example is based on two sibling components. One has a textbox, and one has a button. The button triggers the action to reset the field to an initial value. Thus, the component that fires the event is unaware of the initial value. Also, the component with the textbox is unaware of the button. Both are separated and do not have any link outside a React context. Because the component that fires the action does not store the initial state, it cannot send a string with the initial value as a reset strategy. Instead, we must rely upon the component that will send a beacon to the component responsible for the value.

First of all, the application remains almost the same as previously in terms of actors. The following image shows the user interface. Each green rectangle illustrates a component. div inside a component are purple.


I've removed all the superfluous properties from the two siblings of the previous article. It still has the context provider at the top, allowing it to perform changes and have the underlying component to be notified.

1export default function App() {
2 return (
3 <div className="App">
4 <CommunicationContextProvider>
5 <Parent>
6 <Sibling1 />
7 <Sibling2 />
8 </Parent>
9 </CommunicationContextProvider>
10 </div>
11 );

The goal is that the button residing in <Sibling2 /> makes the <Sibling1 /> textbox to change to its initial value.

The component that initiates the action to reset uses the context. So in that example, it calls context.reset.

1export const Sibling2 = () => {
2 const context = useContext(CommunicationContext);
3 return (
4 <div id="sibling2">
5 <div>Sibling2</div>
6 <div>
7 <button
8 onClick={() => {
9 context.reset();
10 }}
11 >
12 Reset Sibling
13 </button>
14 </div>
15 </div>
16 );

The context has the following contract:

1export interface Communication {
2 reset: () => void;
3 isResetting: boolean;

The reset function invokes the action to reset the text box in the other component. We need to add a second property in the interface: isResetting to indicate to the consumer the reset to reset its text box. The whole idea sits on the boolean value. We will toggle the value at each action. Changing the value from false to true and then false and so on will change the context's value, making the components rely on the context to re-render.

The consumer of the action, the first sibling that has the text box, has a listener on the boolean value of the context.

1export const Sibling1 = () => {
2 const DEFAULT_VALUE = "Sibling 1 default value";
3 const [val, setVal] = useState(DEFAULT_VALUE);
4 const context = useContext(CommunicationContext);
6 useEffect(() => {
8 }, [context.isResetting]);
9 return (
10 <div id="sibling1">
11 <div>Sibling1</div>
12 <div>
13 <input
14 type="text"
15 value={val}
16 onChange={(e) => {
17 setVal(;
18 }}
19 />
20 </div>
21 </div>
22 );

The hook useEffect listens to a change. In our case, we listen to the isResetting. When the value toggles in the context, the effect is called. Inside the effect, we do the actual action to reset the field. The code is clean because the invoker, the Sibling2 is separated from the implementation of the reset logic. Also, the separation removes future modification frictions. For example, another component can subscribe to the notification, or sibling1 can stop relying on the reset. In all cases, the role of sibling2 remains the same and is not impacted by other components' roles.

You can play with the code in