Skip to content

why the component used useAtom will render twice when it has mounted. #1444

Closed Answered by dai-shi
isToThyMoon asked this question in Q&A
Discussion options

You must be logged in to vote

It's the design choice to compute derived atoms in render phase, so that we let React schedule it. (And, also allow Suspense.)
This leads to run render phase twice; first when invalidated, and second when the value is updated. Usually, the second time "bails out", so there's only one commit phase. (On second thought, this case is one render, which bails out without commits.)

When you have a heavy computation, this behavior is a little troublesome. In such cases, heavy computations should be wrapped with useMemo. This is a technique not specific to Jotai.

fwiw, this is an expected behavior of useReducer in React 18 and Jotai intentionally uses this useReducer behavior.

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@isToThyMoon
Comment options

Answer selected by isToThyMoon
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #1443 on September 24, 2022 10:55.