Giter Site home page Giter Site logo

Comments (14)

dai-shi avatar dai-shi commented on May 13, 2024 2

@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.

a-eid avatar a-eid commented on May 13, 2024 1

@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.

dai-shi avatar dai-shi commented on May 13, 2024 1

@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.

italodeandra avatar italodeandra commented on May 13, 2024

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.

a-eid avatar a-eid commented on May 13, 2024

@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.

dai-shi avatar dai-shi commented on May 13, 2024

@a-eid you are welcome,
you can use a context and pass down the atom (not the atom value).

from jotai.

a-eid avatar a-eid commented on May 13, 2024

@dai-shi thank you, you previous comment answered the the query I had in mind.

from jotai.

lemind avatar lemind commented on May 13, 2024

@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.

dai-shi avatar dai-shi commented on May 13, 2024

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.

lemind avatar lemind commented on May 13, 2024

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.

dai-shi avatar dai-shi commented on May 13, 2024

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.

lemind avatar lemind commented on May 13, 2024

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.

dai-shi avatar dai-shi commented on May 13, 2024

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.

lemind avatar lemind commented on May 13, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.