react-event: Event-driven Alternative to React Context
Tags: javascript, react, webdev, showdev
Published: 2023-01-10 19:45:00

```shell
npm i @nucleoidai/react-event
```

`react-event` is an alternative to React Context with event-driven style that helps to build  loosely coupled components.

<center>
  <a href="https://github.com/NucleoidAI/react-event">https://github.com/NucleoidAI/react-event</a>
</center>

---

### How it works?

Subscribers are registered an event with the custom hook `useEvent(eventType, initialValue)`, once publisher posts an event and its payload, `react-event` asynchronously sends the event to subscribed components and subscribed components will eventually be re-rendered with fresh data.


### Hello World :bird:

```javascript
import { publish } from "@nucleoidai/react-event";

const PublishComponent = () => {
  return (
      <button
          onClick={() => {
            publish("BUTTON_CLICKED", { number: 11, string: "red" });
          }}
      >
        Button
      </button>
  );
};
```

```javascript
import { useEvent } from "@nucleoidai/react-event";

const Component1 = () => {
  const initValue = { number: 10 };
  const [event] = useEvent("BUTTON_CLICKED", initValue);

  return <div>{event.number}</div>;
};
```

```javascript
import { useEvent } from "@nucleoidai/react-event";

const Component2 = () => {
  const initValue = { string: "blue" };
  const [event] = useEvent("BUTTON_CLICKED", initValue);

  return <div>{event.string}</div>;
};
```

![Sample react-event](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a9vnbvrwmoiox3pnv20a.gif)
The complete sample project is [here](https://github.com/NucleoidAI/react-event/tree/main/sample).

### Stateless Handling :pill:

`react-event` supports stateless components with caching last published payload for the event type, so that if the component is re-rendered, it won't lose the payload. For example, Component 3 in this example is not re-rendered yet, but `react-event` holds the last payload for the event type, and once the component is rendered, it returns the payload instead of initial value.

![react-event Diagram](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c5mnecm57m7emio8hoxn.png)

## Event-driven Architecture

Event-driven Architecture is commonly used in Microservices systems that pretty much targets similar problem; loose coupling. This style of architecture require middleware like Kafka, RabbitMQ etc. and we are trying to adopt the very same idea to React.js, of course with some modification such as "Stateless Handling".

> :bulb: My personal experience with React Context wasn't pleasant especially, when a project gets bigger. We've been working on [Low-code IDE project](https://github.com/NucleoidAI/IDE), which contains a good amount of reusable components, but they are connected with the giant reducer. We were considering  having multi context reducers concept to ease the problem, seems like it may even complicate more the structure when contexts have to talk to each other.
> ![React Reducer Funny Meme](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/029dek4adlrstyfmncew.png)

## Advanced Usage :raised_hands:

`react-event` can coexist with React Context, actually, it might be even better for complex projects. React Context may handle large dataset with dispatching, which re-renders all listening components (Usually majority of components) and in meanwhile, Synapses can help with local events and limit re-rendering for components that reacting only certain events. This can help to lower workload on a context reducer as well as provide better performance overall.


---

<center>
  Star us on GitHub for the support
  <a href="https://github.com/NucleoidAI/react-event">https://github.com/NucleoidAI/react-event</a>
</center>

[![Nucleoid Logo](https://cdn.nucleoid.com/media/icon.png)](https://nucleoid.com)