How to set initial value to atom when parent component re-render?

136 views Asked by At

Environment

  • react: 18.2
  • jotai: 2.2 (latest)

Problem

const ParentComponent = () => {
    const [val, setVal] = useState(0);

    return <ChildComponent val={val} />;
}


const myAtom = atom(0);
const ChildComponent = ({ val }) => {
    // I want to sync [val -> myAtom]
};

const Child_ChildComponent = () => {
    const valFromAtom = useAtomValue(myAtom);
    ...
};

Should I make new atom every re-render to sync with props ?

useHydrateAtoms did not working.

0

There are 0 answers