English |
中文 |
Русский
(Please contribute translations!)
Unstated Next
200 bytes to never think about React state management libraries ever again
- React Hooks use them for all your state management.
- ~200 bytes min+gz.
- Familiar API just use React as intended.
- Minimal API it takes 5 minutes to learn.
- Written in TypeScript and will make it easier for you to type your React code.
But, the most important question: Is this better than Redux? Well...
- It's smaller. It's 40x smaller.
- It's faster. Componentize the problem of performance.
- It's easier to learn. You already will have to know React Hooks & Context, just use them, they rock.
- It's easier to integrate. Integrate one component at a time, and easily integrate with every React library.
- It's easier to test. Testing reducers is a waste of your time, make it easier to test your React components.
- It's easier to typecheck. Designed to make most of your types inferable.
- It's minimal. It's just React.
So you decide.
See Migration From Unstated docs →
Install
npm install --save unstated-next
Example
{ let count setCount = let let return count decrement increment } let Counter = { let counter = Counter return <div> <button onClick=counterdecrement>-</button> <span>countercount</span> <button onClick=counterincrement>+</button> </div> } { return <CounterProvider> <CounterDisplay /> <CounterProvider initialState=2> <div> <div> <CounterDisplay /> </div> </div> </CounterProvider> </CounterProvider> }
API
createContainer(useHook)
{ let value setValue = let return value onChange } let Container = // Container === { Provider, useContainer }
<Container.Provider>
{ return <ContainerProvider> <ChildComponent /> </ContainerProvider> }
<Container.Provider initialState>
{ let value setValue = // ...} { return <ContainerProvider initialState="value"> <ChildComponent /> </ContainerProvider> }
Container.useContainer()
{ let input = Container return <input value=inputvalue onChange=inputonChange />}
useContainer(Container)
{ let input = return <input value=inputvalue onChange=inputonChange />}
Guide
If you've never used React Hooks before, I recommend pausing and going to read through the excellent docs on the React site.
So with hooks you might create a component like this:
{ let count setCount = let let return <div> <button onClick=decrement>-</button> <p>You clicked count times</p> <button onClick=increment>+</button> </div> }
Then if you want to share the logic behind the component, you could pull it out into a custom hook:
{ let count setCount = let let return count decrement increment } { let counter = return <div> <button onClick=counterdecrement>-</button> <p>You clicked countercount times</p> <button onClick=counterincrement>+</button> </div> }
But what if you want to share the state in addition to the logic, what do you do?
This is where context comes into play:
{ let count setCount = let let return count decrement increment } let Counter = { let counter = return <div> <button onClick=counterdecrement>-</button> <p>You clicked countercount times</p> <button onClick=counterincrement>+</button> </div> } { let counter = return <CounterProvider value=counter> <CounterDisplay /> <CounterDisplay /> </CounterProvider> }
This is great, it's perfect, more people should write code like this.
But sometimes we all need a little bit more structure and intentional API design in order to get it consistently right.
By introducing the createContainer()
function, you can think about your custom hooks as "containers" and have an API that's clear and prevents you from using it wrong.
{ let count setCount = let let return count decrement increment } let Counter = { let counter = Counter return <div> <button onClick=counterdecrement>-</button> <p>You clicked countercount times</p> <button onClick=counterincrement>+</button> </div> } { return <CounterProvider> <CounterDisplay /> <CounterDisplay /> </CounterProvider> }
Here's the diff of that change:
- import { createContext, useContext } from "react"+ import { createContainer } from "unstated-next" function useCounter() { ... } - let Counter = createContext(null)+ let Counter = createContainer(useCounter) function CounterDisplay() {- let counter = useContext(Counter)+ let counter = Counter.useContainer() return ( <div> ... </div> ) } function App() {- let counter = useCounter() return (- <Counter.Provider value={counter}>+ <Counter.Provider> <CounterDisplay /> <CounterDisplay /> </Counter.Provider> ) }
If you're using TypeScript (which I encourage you to learn more about if you are not), this also has the benefit of making TypeScript's built-in inference work better. As long as your custom hook is typed, then everything else will just work.
Tips
Tip #1: Composing Containers
Because we're just working with custom React hooks, we can compose containers inside of other hooks.
{ let count setCount = let let return count decrement increment setCount } let Counter = { let counter = Counter let counter return ...counter reset }
Tip #2: Keeping Containers Small
This can be useful for keeping your containers small and focused. Which can be important if you want to code split the logic in your containers: Just move them to their own hooks and keep just the state in containers.
{ return } let Count = { let count setCount = Count let let let return count decrement increment reset }
Tip #3: Optimizing components
There's no "optimizing" unstated-next
to be done, all of the optimizations you might do would be standard React optimizations.
1) Optimizing expensive sub-trees by splitting the component apart
Before:
{ let counter = Counter return <div> <button onClick=counterdecrement>-</button> <p>You clicked countercount times</p> <button onClick=counterincrement>+</button> <div> <div> <div> <div>SUPER EXPENSIVE RENDERING STUFF</div> </div> </div> </div> </div> }
After:
{ return <div> <div> <div> <div>SUPER EXPENSIVE RENDERING STUFF</div> </div> </div> </div> } { let counter = Counter return <div> <button onClick=counterdecrement>-</button> <p>You clicked countercount times</p> <button onClick=counterincrement>+</button> <ExpensiveComponent /> </div> }
2) Optimizing expensive operations with useMemo()
Before:
{ let counter = Counter // Recalculating this every time `counter` changes is expensive let expensiveValue = return <div> <button onClick=counterdecrement>-</button> <p>You clicked countercount times</p> <button onClick=counterincrement>+</button> </div> }
After:
{ let counter = Counter // Only recalculate this value when its inputs have changed let expensiveValue = return <div> <button onClick=counterdecrement>-</button> <p>You clicked countercount times</p> <button onClick=counterincrement>+</button> </div> }
3) Reducing re-renders using React.memo() and useCallback()
Before:
{ let count setCount = let let return count decrement increment } let Counter = { let counter = Counter return <div> <button onClick=counterdecrement>-</button> <p>You clicked countercount times</p> <button onClick=counterincrement>+</button> </div> }
After:
{ let count setCount = let decrement = let increment = return count decrement increment } let Counter = let CounterDisplayInner = React { let counter = Counter return <CounterDisplayInner ...counter />}
Relation to Unstated
I consider this library the spiritual successor to Unstated. I created Unstated because I believed that React was really great at state management already and the only missing piece was sharing state and logic easily. So I created Unstated to be the "minimal" solution to sharing React state and logic.
However, with Hooks, React has become much better at sharing state and logic. To the point that I think Unstated has become an unnecessary abstraction.
HOWEVER, I think many developers have struggled seeing how to share state and logic with React Hooks for "application state". That may just be an issue of documentation and community momentum, but I think that an API could help bridge that mental gap.
That API is what Unstated Next is. Instead of being the "Minimal API for sharing state and logic in React", it is now the "Minimal API for understanding shared state and logic in React".
I've always been on the side of React. I want React to win. I would like to see the community abandon state management libraries like Redux, and find better ways of making use of React's built-in toolchain.
If instead of using Unstated, you just want to use React itself, I would highly encourage that. Write blog posts about it! Give talks about it! Spread your knowledge in the community.
unstated
Migration from I've intentionally published this as a separate package name because it is a complete reset on the API. This way you can have both installed and migrate incrementally.
Please provide me with feedback on that migration process, because over the next few months I hope to take that feedback and do two things:
- Make sure
unstated-next
fulfills all the needs ofunstated
users. - Make sure
unstated
has a clean migration process towardsunstated-next
.
I may choose to add APIs to either library to make life easier for developers. For unstated-next
I promise that the added APIs will be as minimal as possible and I'll try to keep the library small.
In the future, I will likely merge unstated-next
back into unstated
as a new major version. unstated-next
will still exist so that you can have both unstated@2
and unstated-next
installed. Then when you are done with the migration, you can update to unstated@3
and remove unstated-next
(being sure to update all your imports as you do... should be just a find-and-replace).
Even though this is a major new API change, I hope that I can make this migration as easy as possible on you. I'm optimizing for you to get to using the latest React Hooks APIs and not for preserving code written with Unstated.Container
's. Feel free to provide feedback on how that could be done better.