Hacker News new | ask | show | jobs
by gulugawa 42 days ago
How do you decide to use useContext, useReducer, useState, or a third party management tool to manage state?

(I know useContext isn't great for state management, but I've worked on a web application where useContext was used to store complex global state).

3 comments

My main complaint is that React doesn't handle highly dynamic situations. Think of how tools like photoshop or programming IDEs have tons of views and windows and property sheets and stuff and manage to update the right parts of the UI when things change. React on the other hand makes a structural link between the component tree and how state propagates.

Specifically I wrote a bunch of React components for making little biosignals applications that can (say) show two people's heartrate from bluetooth LE and show my breathing based on a strap i am wearing and another person's breathing based on a $20 radar from China.

I can pretty easily snap together the components and the system that feeds the state to the components by writing code. It works great, it's not that hard to do, it looks great.

But: I really wish I could make something where I could drag and drop display and data acquisition and processing components like LabView. Actually I know a lot about how to do the dynamic processing (Hint: read the Dragon book, not On Lisp) but React doesn't support dynamically assembled components... But I know Javascript systems can because I was writing them 2005-2010 back when browsers didn't have async and all the great affordances they offer now.

I'm not a fan of state management in React apps, and it took me a long time to come to peace with it. What I landed on that works with the system rather than against is useContext at the page level containing Jotai atoms that wrapper Immer-managed objects representing the page states that get passed through the component tree as props.

I built my own action framework that gives me the ability to use Jotai getters to read atom data, launch asynchronous javascript, and then write to atom data via Jotai setters without ever having to fuss with useEffect myself. Jotai just handles the messy state transition work. My components used to be a jumble of DOM event handler, business logic, and markup, and now the business logic is all extracted to the separate action components.

React makes it hard to test business logic in isolation, and I am hoping my action framework could do a better job of that.

I tried using Jotai, and thought it was a great way to manage state. In contrast, useContext seems like an overcomplication with less control over rendering from state updates.

Is your action framework open source? I'd be interested in taking a look at it.

useState for local state or useContext (+ useReducer if it's complicated) for shared state is the "built-in" way that should be good enough for most things.