DEV Community

Cover image for Managing Application State with Custom Events in React: A Simple Yet Powerful Approach

Managing Application State with Custom Events in React: A Simple Yet Powerful Approach

Adrian Knapp on January 31, 2025

When building React applications, managing state across components can become challenging. While solutions like Context API, Redux, or Zustand are ...
Collapse
 
gunslingor profile image
gunslingor • Edited

Just an FYI, the event paradigm in react becomes highly problematic. It's just as bad as combining angular and react in a real world application. They know nothing of each other and you end up with the worst of both worlds. Architecture is the solution, but this solution becomes an uneconomical problem, as it usually does when you bypass the original intent and integrations your main framework vendor intended you to use.

That being said, it is a good implementation since it's isolated, I usually see all this inside the main 800 line react function, lol. Really though, they should be converted to state hooks and lifted if appropriate. Remember react has a virtual dom, not the browser dom... your editing the browser dom there I think without telling react... so the react state becomes invalid. Worst, human devs end up having no idea what's happening. I love events... but if you use em, definitely you shouldn't be using react... they work very well with pure js, very bad with react, thee hath been warned... one need not two systems of managing events.

Collapse
 
traviselam profile image
Travis Elam (wisconsinite) • Edited

New devs should pay attention to this ^ comment

Collapse
 
genexu profile image
Gene Xu

True, the first thing I learned when I was React noob developer is always to keep state and VDOM consistent with React itself and don't try to control elements without React data flow.

Collapse
 
ansa70 profile image
Enrico Ansaloni

I used this approach years ago on an old class based React app, here's a few things I learned:

  • it's a good thing to create a singleton type class to handle global events dispatch and handling
  • be very careful to unsubscribe from events before the component lifecycle ends or you'll end up with nasty side effects and possibly memory leaks
  • create an utility class to handle event subscription/unsubscription in a common centralized way so you can use it in all components and have a logging/debugging singular point
  • create structured types and/or Interfaces to pass as events for a more robust approach
Collapse
 
adrianknapp profile image
Adrian Knapp

Well said, @ansa70! All these points are very important. I think I covered each one in my implementation. Did you notice if I forgot something?

Collapse
 
syeo66 profile image
Red Ochsenbein (he/him)

Maybe you'd want to take a look at useSyncExternalStore

Collapse
 
adrianknapp profile image
Adrian Knapp

Nice, thanks for the recommendation, I'm gonna take a look!

Collapse
 
xyzt70 profile image
Mohamad Yahia • Edited

This works for smaller/hobby apps but it's much easier to scale if you just use zustand as the single source of truth.

Collapse
 
matheusrufca profile image
Matheus Rufca

You may need to add some complexity if you want to handle multiple same-name events in the same component.

Collapse
 
jeanluca999 profile image
Jean

Interesting, even though I knew browser custom events, I never considered the potential to use them to handle state in React apps, it's the kind of knowledge that is worth having in my pocket

Collapse
 
adrianknapp profile image
Adrian Knapp

Absolutely, Jean! It's very useful and you can customize whatever you want. Glad this helped you, thanks for reading!

Collapse
 
reactoholic profile image
Petar Kolev

Dude, do not trick people to fall in this trap... React is not Node. Event driven React is one of the most React ani-pattern things to implement and it's hell to debug in a bigger app. I've been working on such app and believe me, I'm not going there again.
Nice of you to put effort in this article but I highly recommend you to research very good what you want to write about before posting it.

Collapse
 
alveshelio profile image
Helio Alves

I don't see any added benefits, only drawbacks. @gunslingor described really well all the problems you will encounter with this approach.
You are basically re-implementing React's internals.

If you want to go with this approach you shouldn't be using React.

Collapse
 
netssrmrz profile image
Esteban Ramirez

Good article. Custom events are a powerful mechanism that I use all the time. It's sad to see React fanboys refer to this as an anti-pattern. You're missing out guys. There's plenty other DOM concepts that React components don't support, like simple properties and methods. This shouldn't be considered a feature. It's a limitation. Don't believe the React hype that it's for your own good. Bad devs will make a mess of anything. Even React.

Collapse
 
nathandaly profile image
Nathan Daly

Isn't this what a store is for?