Synapses: Event-driven Alternative to React Context

npm i @nucleoidjs/synapses

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

https://github.com/NucleoidJS/Synapses

How it works?

Subscribers are registered a…


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Can Mingir

npm i @nucleoidjs/synapses

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


https://github.com/NucleoidJS/Synapses

How it works?

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

Hello World 🐦

import { publish } from "@nucleoidjs/synapses";

const PublishComponent = () => {
  return (
      <button
          onClick={() => {
            publish("BUTTON_CLICKED", { number: 11, string: "red" });
          }}
      >
        Button
      </button>
  );
};
import { useEvent } from "@nucleoidjs/synapses";

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

  return <div>{event.number}</div>;
};
import { useEvent } from "@nucleoidjs/synapses";

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

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

Sample Synapses
The complete sample project is here.

Stateless Handling 💊

Synapses 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 Synapses holds the last payload for the event type, and once the component is rendered, it returns the payload instead of initial value.

Synapses Diagram

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".

💡 My personal experience with React Context wasn't pleasant especially, when a project gets bigger. We've been working on Low-code IDE project, 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

Advanced Usage 🙌

Synapses 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.


Star us on GitHub for the support
https://github.com/NucleoidJS/Synapses

Nucleoid Logo


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Can Mingir


Print Share Comment Cite Upload Translate Updates
APA

Can Mingir | Sciencx (2023-01-10T19:45:00+00:00) Synapses: Event-driven Alternative to React Context. Retrieved from https://www.scien.cx/2023/01/10/synapses-event-driven-alternative-to-react-context/

MLA
" » Synapses: Event-driven Alternative to React Context." Can Mingir | Sciencx - Tuesday January 10, 2023, https://www.scien.cx/2023/01/10/synapses-event-driven-alternative-to-react-context/
HARVARD
Can Mingir | Sciencx Tuesday January 10, 2023 » Synapses: Event-driven Alternative to React Context., viewed ,<https://www.scien.cx/2023/01/10/synapses-event-driven-alternative-to-react-context/>
VANCOUVER
Can Mingir | Sciencx - » Synapses: Event-driven Alternative to React Context. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2023/01/10/synapses-event-driven-alternative-to-react-context/
CHICAGO
" » Synapses: Event-driven Alternative to React Context." Can Mingir | Sciencx - Accessed . https://www.scien.cx/2023/01/10/synapses-event-driven-alternative-to-react-context/
IEEE
" » Synapses: Event-driven Alternative to React Context." Can Mingir | Sciencx [Online]. Available: https://www.scien.cx/2023/01/10/synapses-event-driven-alternative-to-react-context/. [Accessed: ]
rf:citation
» Synapses: Event-driven Alternative to React Context | Can Mingir | Sciencx | https://www.scien.cx/2023/01/10/synapses-event-driven-alternative-to-react-context/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.