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,
});
CodePudding user response:
You need to combine the previous state and updated state like this
setWallet((prevState) => ({
...prevState,
connected: true,
}));