React
A simple, principled way to manage shared state inHooks API
Built on React's newatoms in reagent.cljs
Inspired byDisclaimer: the React Hooks API is currently only a proposal, therefore this library should be considered experimental and unfit for production apps at this time
- Description
- Why use
react-atom
? - Installation
- Documentation
- Code Example:
react-atom
in action - 🕹️ Play with
react-atom
in CodeSandbox 🎮️ - Contributing / Feedback
Description
react-atom
provides a simple way to manage state in React, for both global app state and for local component state: ✨Atom
s✨
Atom
:
Put your state in an import { Atom } from "@dbeining/react-atom";
const appState = Atom.of({
color: "blue",
userId: 1
});
deref
Read state with You can't inspect Atom
state directly, you have to deref
erence it, like this:
import { deref } from "@dbeining/react-atom";
const { color } = deref(appState);
swap
Update state with You can't modify an Atom
directly. The main way to update state is with swap
. Here's its call signature:
function swap<S>(atom: Atom<S>, updateFn: (state: S) => S): void;
updateFn
is applied to atom
's state and the return value is set as atom
's new state. There are just two simple rules for updateFn
:
- it must return a value of the same type/interface as the previous state
- it must not mutate the previous state
To illustrate, here is how we might update appState
's color:
import { swap } from "@dbeining/react-atom";
const setColor = color =>
swap(appState, state => ({
...state,
color: color
}));
Take notice that our updateFn
is spreading the old state onto a new object before overriding color
. This is an easy way to obey the rules of updateFn
.
swap
Side-Effects? Just use You don't need to do anything special for managing side-effects. Just write your IO-related logic as per usual, and call swap
when you've got what you need. For example:
const saveColor = async color => {
const { userId } = deref(appState);
const theme = await post(`/api/user/${userId}/theme`, { color });
swap(appState, state => ({ ...state, color: theme.color }));
};
useAtom
✨ to subscribe to Atom state in components
✨useAtom
is a custom React Hook. It does two things:
- returns the current state of an atom (like
deref
), and - subscribes your component to the atom so that it re-renders every time its state changes
It looks like this:
export function ColorReporter(props) {
const { color, userId } = useAtom(appState);
return (
<p>
User {userId} has selected {color}
</p>
);
}
Nota Bene: You can also subscribe to computed state by using the
options.select
argument. Read the docs for details.
react-atom
?
Why use 😌 Tiny API / learning curve
A total of five functions, and most of the time you'll only need three of them.
🚫 No boilerplate, just predictable state management
Reducers? Actions? Thunks? Sagas? Forget about it.
🎵 Tuned for performant component rendering
TheuseAtom
hook accepts an optionalselect
function that lets components subscribe to computed state. That means the component will only re-render when the value returned fromselect
changes.
😬 React.useState
doesn't play nice with React.memo
useState
is cool until you realize that in most cases it forces you to pass new function instances through props on every render because you usually need to wrap thesetState
function in another function. That makes it hard to take advantage ofReact.memo
. For example:---function Awkwardddd(props) { const [name, setName] = useState(""); const [bigState, setBigState] = useState({ ...useYourImagination }); const updateName = evt => setName(evt.target.value); const handleDidComplete = val => setBigState({ ...bigState, inner: val }); return ( <> <input type="text" value={name} onChange={updateName} /> <ExpensiveButMemoized data={bigState} onComplete={handleDidComplete} /> </> ); }Every time
input
firesonChange
,ExpensiveButMemoized
has to re-render becausehandleDidComplete
is not strictly equal (===) to the last instance passed down.The React docs admit this is awkward and suggest using Context to work around it, because the alternative is super convoluted.
With
react-atom
, this problem doesn't even exist. You can define your update functions outside the component so they are referentially stable across renders.const state = Atom.of({ name, bigState: { ...useYourImagination } }); const updateName = ({ target }) => swap(state, prev => ({ ...prev, name: target.value })); const handleDidComplete = val => swap(state, prev => ({ ...prev, bigState: { ...prev.bigState, inner: val } })); function SoSmoooooth(props) { const { name, bigState } = useAtom(state); return ( <> <input type="text" value={name} onChange={updateName} /> <ExpensiveButMemoized data={bigState} onComplete={handleDidComplete} /> </> ); }
TS First-class TypeScript support
react-atom
is written in TypeScript so that every release is published with correct, high quality typings.
⚛️ Embraces React's future with Hooks
Hooks will makeclass
components and their kind (higher-order components, render-prop components, and function-as-child components) obsolete.react-atom
makes it easy to manage shared state with just function components and hooks.
Installation
react-atom
has zero bundled dependencies
and only two peerDependency
,
namely, react@^16.7.0-alpha.0
and react-dom@^16.7.0-alpha.0
, which contain
the new Hooks API.
npm i -S @dbeining/react-atom react@^16.7.0-alpha.0 react-dom@^16.7.0-alpha.0
Documentation
You can find API docs for react-atom
here
react-atom
in action
Code Example: Click for code sample
import React from "react";
import ReactDOM from "react-dom";
import { Atom, useAtom, swap } from "@dbeining/react-atom";
//------------------------ APP STATE ------------------------------//
const stateAtom = Atom.of({
count: 0,
text: "",
data: {
// ...just imagine
}
});
//------------------------ EFFECTS ------------------------------//
const increment = () =>
swap(stateAtom, state => ({
...state,
count: state.count + 1
}));
const decrement = () =>
swap(stateAtom, state => ({
...state,
count: state.count - 1
}));
const updateText = evt =>
swap(stateAtom, state => ({
...state,
text: evt.target.value
}));
const loadSomething = () =>
fetch("https://jsonplaceholder.typicode.com/todos/1")
.then(res => res.json())
.then(data => swap(stateAtom, state => ({ ...state, data })))
.catch(console.error);
//------------------------ COMPONENT ------------------------------//
export const App = () => {
const { count, data, text } = useAtom(stateAtom);
return (
<div>
<p>Count: {count}</p>
<p>Text: {text}</p>
<button onClick={increment}>Moar</button>
<button onClick={decrement}>Less</button>
<button onClick={loadSomething}>Load Data</button>
<input type="text" onChange={updateText} value={text} />
<p>{JSON.stringify(data, null, " ")}</p>
</div>
);
};
ReactDOM.render(<App />, document.getElementById("root"));
react-atom
in CodeSandbox 🎮️
🕹️ Play with You can play with react-atom
live right away with no setup at the following links:
JavaScript Sandbox | TypeScript Sandbox |
---|---|
Contributing / Feedback
Please open an issue if you have any questions, suggestions for improvements/features, or want to submit a PR for a bug-fix (please include tests if applicable).