question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Initial atom state

See original GitHub issue

Currently the most issue I see with atoms is initial atom state which especially on server cant be inside any global objects ie localStorage etc. (setEffect is not working on server too)

Creating atoms and passing them through a context - loss of main advantages (u would need to create dependent atoms in context etc)

Probably passing Provider value to atom on initial call could solve this?

I.e.

// atom already have getter fn as param so I call atom with state dependent on context subatom.

const myAtom = subatom(ctx => ctx.someVal)

....
// in some component

<InitialJotaiValueProvider value={{ someVal: 123 }}> ...

Or probably some other beautiful api can be available.

PS: Api like above can be typed well like

const { InitialValueProvider, subatom} = createBlaBla<ValueType>()

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
dai-shicommented, Oct 9, 2020

You are welcome!

For someone interested, it’s like this.

const weakAtomFamily = (initializeRead, initializeWrite) => {
  const atoms = new WeakMap()
  return (obj) => {
    if (atoms.has(obj)) return atoms.get(obj)
    const newAtom = atom(initializeRead(obj), initializeWrite && initializeWrite(obj))
    atoms.set(obj, newAtom)
    return newAtom
  }
}
0reactions
dai-shicommented, Nov 1, 2020

#158 added shouldRemove. There can still be cases weakAtomFamily works better. We can add it to utils if someone suggests some use cases.

For now, I don’t think we have any actions for this issue, so closing this.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Initializing Atom State on Render - Jotai
There are times when you need to create an reusable component which uses atoms. These atoms' initial state are determined by the props...
Read more >
atom(options) - Recoil
An atom represents state in Recoil. The atom() function returns a writeable RecoilState object. ... default - The initial value of the atom....
Read more >
What is the initial excited state of the atom? - Toppr
The quantum number n corresponding to the excited state of He+ion if on transition to the ground state that ion emits two photons...
Read more >
Atom - Wikipedia
The nucleus is made of one or more protons and a number of neutrons. Only the most common variety of hydrogen has no...
Read more >
A hydrogen atom is in the initial state { n_i = n }, where n1. A ...
Answer to: A hydrogen atom is in the initial state { n_i = n }, where n1. A. Find the frequency of the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found