by davidmccabe · 6y ago · 0 comments
In other words, this updates both of the atoms together:
const [a, setA] = useRecoilState(atomA);
const [b, setB] = useRecoilState(atomB);
...
onClick={() => {
setA(a => a + 1);
setB(b => b + 1);
}}
If the new values of multiple atoms are interdependent on each others' current values, it's possible to update multiple atoms together using the transactional/updater/function form, but we need to add a nicer interface for doing this. Coming soon! The nicer interface would probably look something like this: const update = useTransactionalUpdate([atomA, atomB]);
...
onClick={() => {
update(([a, b]) => [a + b, b - a]);
}}
It's then easy to make a hook that provides a reducer-style interface over a set of atoms. But now, unlike with Redux or useReducer, each atom still has its own individual subscriptions!