Testing React I was thinking how to resolve problem with emit events between components.

One of my examples was to create two components:

Header Content

In Header component I have simple button with method refreshContent to simulate event in the Content component view after button click in Header component.

So let’s start with Header component.

Ok, as you can see, this React Component renders view with a button. On click event we emit event by this.emitContentUpdate().

Let’s write simple Content component to accept our event.

As you can see, out Content component has extra method onRefreshContent (the same name as emitted event name refreshContent). Now every time we will emit this.emitRefreshContent() we will fire onRefreshContent method. Easy.

So the best on the end. Let’s write simple event emitter to support this idea.

Works like a charm. Let me know what do you think about it.