'How to update Recoil JS Atom Object

I have an Atom object defined like this:

export const walletInfo = atom({
  key: "walletInfo",
  default: {
    connected: false,
    someString: "",
    someInt: 0,
    someOtherInfo: "",
  },
});

The question is, how do I change only single value in Atom object without overwriting everything else? For example, how do I set connected: true and keep the rest of the information?

The code below will "erase" the rest of the object

import { walletInfo } from "../src/atoms";
const [wallet, setWallet] = useRecoilState(walletInfo);

setWallet({
   connected: true,
});


Solution 1:[1]

You need to combine the previous state and updated state like this

setWallet((prevState) => ({
   ...prevState,
   connected: true,
}));

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Nick Vu