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.

[Tips] Updating atom object property

See original GitHub issue

Atoms can hold any primitive values, including objects. Here’s an example to updating a property of an object.

const funkyPigAtom = atom({ funkyPig: { glasses: 'stylish', bling: 'golden' }})

const Component = () => {
  const [funkyPig, setFunkyPig] = useAtom(funkyPigAtom)
  const updateBling = (bling) => {
    setFunkcyPig((prev) => ({ ...prev, funkyPig: { ...prev.funkyPig, bling } }))
  }
  // ...
}

We could also create an update action atom.

const funkyPigAtom = atom({ funkyPig: { glasses: 'stylish', bling: 'golden' }})
const updateBling = atom(
  null,
  (get, set, bling) => {
    const prev = get(funkyPigAtom)
    set(funkyPigAtom, { ...prev, funkyPig: { ...prev.funkyPig, bling } })
  }
)

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
NorweskiDrwalcommented, Sep 11, 2020

Ooooh, you guys are the best! I am learning about Observer Patterns in JS and wanted to write a state management lib using OP for giggles, so I found this, which led me here (and to Recoil) and now I’m just writing a handlers layer on top of Jotai to learn how stuff works. @dai-shi thanks a lot for doing all this 😉

1reaction
antipalindromecommented, Sep 11, 2020

@dai-shi You’re way ahead of me, thanks! 😃 Maybe I should spend more time looking through all these issue!

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to update Recoil JS Atom Object - Stack Overflow
You need to combine the previous state and updated state like this setWallet((prevState) => ({ ...prevState, connected: true, }));.
Read more >
Updating JavaScript Object Property - React Tips
The solution is to copy object : const newObject = {...object}; and then update value of a copy: newObject[property] = "Updated value"; and...
Read more >
atom(options) - Recoil
The atom() function returns a writeable RecoilState object. ... If a selector is used as the default the atom will dynamically update as...
Read more >
How do you rerender a React component when an object's ...
I have an object in my React application which is getting updated from another system. I'd like to display the properties of this...
Read more >
How to define custom atom names
In the diagram, right-click the descriptor for the object you want to customize atom names for and select Properties to open the Object...
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