Comments (14)
@a-eid Can you create a codesandbox example to show what you would like to do? Even if it's incomplete. Then, we could help based on it.
What you described sounds like something possible with useState.
I wanna hold its state in atoms rather than react state to avoid unnecessary re-renders.
OK, now I see your point.
const Input = () => {
const validAtom = useRef(atom(false)).current
const charCountAtom = useRef(atom(0)).current
function handleChange() {
// ... handle change
}
return <div>
<InputIcon validAtom={validAtom} charCountAtom={carCountAtom} />
<InputLabel validAtom={validAtom} charCountAtom={carCountAtom} />
<BaseInput validAtom={validAtom} charCountAtom={AtomcarCountAtom} onChange={handleChange}/>
<inputCountIndicator validAtom={validAtom} charCountAtom={carCountAtom} />
... etc.
</div>
}
Something like this?
There can be various patterns.
Explicit lazy ref initialization:
const validAtom = useRef()
if (!validAtom.current) {
validAtom.current = atom(false)
}
Create multiple atoms in a ref:
const atoms = useRef()
if (!atoms.current) {
atoms.current = {
valid: atom(false),
charCount: atom(0),
}
}
Instead of useRef, we could use useState, useMemoOne, or even useAtom.
We could also use atomFamily. It's a variant of singleton, but it would work for this use case.
It would be still worth for a codesandbox example, so that we can discuss more concretely, and have better ideas.
from jotai.
@italodeandra yes they do, I'm just wondering what would be the best way to create/consume atoms in a non singleton way.
to clarify what I want is:
some stateful reusable components are too big to re-render for each change the happens, I wanna hold it's state in atoms and consume it where needed.
for example
const Input = () => {
const [valid, setValid] = React.useState(false)
const [charCount setCharCount] = React.useState(0)
function handleChange() {
// ... handle change
}
return <div>
<InputIcon valid={valid} charCount={carCount} />
<InputLabel valid={valid} charCount={carCount} />
<BaseInput valid={valid} charCount={carCount} onChange={handleChange}/>
<inputCountIndicator valid={valid} charCount={carCount} />
... etc.
</div>
}
say this component will be needed all over the place
I wanna hold its state in atoms rather than react state to avoid unnecessary re-renders.
what would be the best way to do so ( consuming / creating non singleton state in atoms )
@dai-shi am really interested in your opinion
thanks.
from jotai.
@a-eid Do you have any other questions? If we have concrete ideas, we could write something in docs.
#5 is a related issue.
from jotai.
I didn't completely get it what you're looking for. But I know atoms can be created on render, perhaps that can help.
from jotai.
@dai-shi thank you,
if I create an atom from within the component ( on the fly ), how do u think it's best to use it outside / from deep within the component tree ?
from jotai.
@a-eid you are welcome,
you can use a context and pass down the atom (not the atom value).
from jotai.
@dai-shi thank you, you previous comment answered the the query I had in mind.
from jotai.
@dai-shi
I have related question please
How can we make atom singleton without hooks nor familyAtom?
And little one about store
const myStore = createStore()
const countAtom = atom(0)
myStore.set(countAtom, 1)
We can set value like this.
But what I want to use as a key one atom and save value like another atom?
is that possible?
Thank you
from jotai.
But what I want to use as a key one atom and save value like another atom?
I'm afraid I don't get the question. What's the imaginary usage?
from jotai.
But what I want to use as a key one atom and save value like another atom?
I'm afraid I don't get the question. What's the imaginary usage?
// one file
const getAtom = (params) => atom({bigAtom: params})
const newAtom = getAtom()
myStore.set(newAtom, newAtom)
// another file
// to get the newAtom value from myStore we need atom key, but we can not get it, cause we get an atom via getAtom function
The overall question that if we create atom inside function and can not export it then it hard to use it as a key for a store.
from jotai.
Not sure, but the only workaround is to export a function from the first file?
Not even sure if it's Jotai specific problem.
In the mental model, assume the following:
function atom(init) {
return { init }
}
function createStore() {
return new WeakMap()
}
from jotai.
Not sure, but the only workaround is to export a function from the first file? Not even sure if it's Jotai specific problem. In the mental model, assume the following:
function atom(init) { return { init } } function createStore() { return new WeakMap() }
Even if we export getAtom
fn, with new call it will be another atom, am I understand correct?
I need something like - we create atom dynamically then put it to a Store via may be a string key myStore.set('meAtom', newAtom)
then we get it in another file by the key.
So I just need something like singleton atom. As you said before I can use useState,etc, but may be I can solve my issue out of react cmp. And I missed some workaround.
I hope it is clearer. Thank you
from jotai.
Something like this?
// one file
export const myStore = createStore()
export const newAtomAtom = atom(null)
const getAtom = (params) => atom({bigAtom: params})
const newAtom = getAtom()
myStore.set(newAtomAtom, newAtom)
// another file
const newAtom = myStore.get(newAtomAtom)
const value = myStore.get(newAtom)
from jotai.
Something like this?
// one file export const myStore = createStore() export const newAtomAtom = atom(null) const getAtom = (params) => atom({bigAtom: params}) const newAtom = getAtom() myStore.set(newAtomAtom, newAtom) // another file const newAtom = myStore.get(newAtomAtom) const value = myStore.get(newAtom)
There thing is with TS you should support null
option.
Let me work around your solution.
Thank you
from jotai.
Related Issues (20)
- (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
- Nextjs 14 app router webpack error
- Idea: lazy initialization for primitive atoms HOT 17
- Investigate and fix possible memory leaks in `store2` HOT 6
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.