Replies: 1 comment 2 replies
-
There are a few different ways to make atom data accessible outside of the React tree. Sync the atom value with a global variable
import { atom } from 'jotai';
import type { WritableAtom } from 'jotai';
export const atomWithGlobal = (getVar: any, setVar: any): WritableAtom<Promise<never>, unknown> => {
const baseAtom = atom(getVar());
const anAtom = atom(
(get) => get(baseAtom),
(get, set, update) => {
const newValue = typeof update === 'function' ? update(get(baseAtom)) : update;
set(baseAtom, newValue);
setVar(newValue);
},
);
return anAtom;
}; Two-way data binding with a store or proxy
|
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I created a demo here - https://codesandbox.io/s/objective-kepler-2gl0n?file=/src/App.js
I want to make the state of the atom
numAtom
accessible from outside. This is the technique I'm using:{current: null}
and mutate that whenever thenumAtom
changes.Beta Was this translation helpful? Give feedback.
All reactions