Comments (11)
@hsw107 You can define atoms anywhere in any files, export them, and import them where needed. If you are asking some best practices to organize atoms in files, it's a good question. We'd like to collect various use cases and find a good pattern.
from jotai.
return [...useAtom(dataAtom), dataAtom] as const;
would also work.
from jotai.
I would love to see an example of updating properties of a stored object.
const funkyPigAtom = atom({ funkyPig: { glasses: 'stylish', bling: 'golden' }})
Say I wanna update bling
.
from jotai.
@NorweskiDrwal Hi, I made a new Tips issue. Please check: #32
from jotai.
@mmiller42
It should work. (More precisely, it's fixed since v0.7.0. Prior to that version, dependencies are accumulated in each invocation of get
.)
Note condition
should come from some other atoms for proper tracking. If it's just a global variable, the behavior would become just random..
Please also follow the discussion in #66. I need your feedbacks.
I figured it tracks the dependencies the first time it's evaluated
It will track them every time it's evaluated.
from jotai.
This is just a first draft. Feedback is very welcomed to improve it.
from jotai.
I would love to see clarification/documentation on how to use atoms across multiple files. So far I'm using one file that contains all the atoms and I import the individual atoms I need into my component files, but not sure if that's the best way.
from jotai.
OTOH if one wants an atom to be completely resident within the component including the atom config, sort of like the React's useState
, a custom hook like this can be useful:
const useCreateAtom = <Value extends any>(initialValue: NonFunction<NonPromise<Value>>) => {
const [dataAtom] = useState(() => atom(initialValue));
return useAtom(dataAtom);
};
function TodoItem({idx}: { idx: number }) {
const [data, setData] = useCreateAtom({id: idx, done: false});
}
Edit 1: @dai-shi The above code works fine but when I try to use the below code, it doesn't work. Please suggest how to fix it. setData
is no longer callable because it is now a union of types of data
, setData
and dataAtom
.
const useCreateAtom = <Value extends any>(initialValue: NonFunction<NonPromise<Value>>) => {
const [dataAtom] = useState(() => atom(initialValue));
return [...useAtom(dataAtom), dataAtom];
};
function TodoItem({idx}: { idx: number }) {
const [data, setData, dataAtom] = useCreateAtom({id: idx, done: false});
}
Edit 2: Manually specifying return types work, not sure why TS was not able to infer the return type
const useCreateAtom = <Value extends any>(initialValue: NonFunction<NonPromise<Value>>): [NonPromise<Value>, (update: SetStateAction<Value>) => void, PrimitiveAtom<Value>] => {
const [dataAtom] = useState(() => atom(initialValue));
return [...useAtom(dataAtom), dataAtom];
};
from jotai.
Could you clarify this part of the docs?
The useAtom hook can be seen as a special useContext hook. It returns an atom value stored in the Provider and an updating function as a tuple, just like useState.
The way it reads it makes it seem like the registration of an atom to the Provider is done somewhere else. Based on the code it seems like it would be more fair to say something like this...
atom
is a function to create an atom config. It's an object and the object identity is important. You can create it from everywhere, and must register it to a Jotai Provider
after creation. To do so, you must use one of the built in hooks to register it to a Jotai Provider
.
Amazing work on the library! I needed this exact thing for a rich text editor to manage the state of each created block. Since the identity of the atom
s are important would this be an acceptable pattern for registering an atom per component? I want it to live in render so that each time I invoke <Component />
in the code it has its own atom state. Could be a good thing to capture based on #27 (comment).
const Component = () => {
const atomRef = React.useRef(atom(0))
const [count, setCount] = useAtom(atomRef.current)
return <div>something</div>
}
from jotai.
Thanks for your feedback!
can be seen as a special useContext hook
I wanted to make an analogy to typical Context+useState usage. Maybe I should add some code.
it seem like the registration of an atom to the Provider is done somewhere else.
I see. We need to add some notes then.
and must register it to a Jotai Provider after creation. To do so,
The registration is rather implicit, so it's not a user who registers explicitly, I'd say. Let me try to incorporate.
Amazing work on the library! I needed this exact thing for a rich text editor to manage the state of each created block.
Would love to see what would build (even a small example).
const atomRef = React.useRef(atom(0))
This is valid, but this is equivalent to React.useState(0)
because refs are gone on unmount.
Is it what you want?
from jotai.
Can you clarify how the get
function subscribes to updates for a derived atom? For example, I assume something like this wouldn't work right?
atom(get => get(condition ? atom1 : atom2))
though I'm not sure (I figured it tracks the dependencies the first time it's evaluated)
from jotai.
Related Issues (20)
- useHydrateAtoms conflicts with useAtomsDebugValue HOT 3
- react-refresh-plugin crashes if createStorage is even referenced in a file running on expo web HOT 1
- Cann't access `atomWithStorage` value in spa initial HOT 14
- (nextjs) - useHydrateAtoms return previous value on dynamic route
- Document the use of a function as the second parameter of `set` HOT 2
- Vite 5 / Rollup 4 build warning in v2.6.0 HOT 14
- "TypeError: Invalid value used as weak map key" when using helper function to create atoms.
- How do I use jotai in ordinary js files?not function component.
- [React Native] Atom updates that occur within setTimeout/setInterval trigger a re-render for every useAtom instance HOT 1
- mount is executed twice HOT 2
- Bad lingature for coding font on doc page
- Atoms remount on recalculations HOT 1
- (Testing) HydrateAtoms component resets atoms when using multiple initial values
- RFC: Add `atomWithRefresh` in `jotai/utils` HOT 4
- react re-render fire just init time HOT 1
- Confusing example in documentation HOT 1
- Jotai suspends unnecessarily when a fullfilled promise is used by a derived atom
- atomWithStorage not working in ReactNative HOT 1
- webpack support
- Fix Support Page HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from jotai.