Skip to content

Update atom/selector data independent of view instances #1115

Answered by drarmstr
rumax asked this question in Q&A
Discussion options

You must be logged in to vote

It's a good practice to try to have a single source of truth for state. No need for maintaining a separate atom if the session is active if you also have an atom for the remaining time; that could be derived state. For example:

export const remainingTimeState = atom<number>({
  key: 'remainingTime',
  default: SESSION_TIMEOUT,
});

export const isSessionActiveState = selector<boolean>({
  key: 'isSessionActive',
  get: ({get}) => get(remainingTimeState) > 0,
});

As you mention, you shouldn't be setting state from selector accessors or render functions. You can update the countdown from a React effect or other async callback.

function MyComponent() {
  const startSession = useRecoilCallback((

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by drarmstr
Comment options

You must be logged in to vote
0 replies
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 #1110 on July 12, 2021 22:08.