logoalt Hacker News

gulugawayesterday at 8:32 PM1 replyview on HN

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


Replies

dwoldrichyesterday at 9:09 PM

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.