1
const tempFahrenheit = atom({
    key: 'tempFahrenheit',
    default: 32
  });

  const [tempF, setTempF] = useRecoilState(tempFahrenheit);

  const handleClick = (temp: number) => {
    flushSync(() => {
      setTempF(temp);
    });
    flushSync(() => {
      console.log(tempF);
    });
  };

The above displays 32 when onClick handler is called. So if we had a recoil selector using the ´tempFahrenheit´ atom and try to use it in the onClick handler, it takes the older value 32.

Tried to opt out of batching with React 18 with flush sync, but still fails getting the latest value.

Jackson
  • 11
  • 2

0 Answers0