Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

105
Visualizações
can we update an atom state whenver another atom changes in RecoilJS

im quite new to recoiljs atom and stuff. Assumming i have 2 atoms

timeRangeAtom which determines the time range of the selection

export const timeRangeAtom = atom<Array<string>>({
   key: 'timeRangeAtom',
   default: [
       dayjs().subtract(7, 'day').format(DateFormat),
       dayjs().subtract(1, 'day').format(DateFormat),
       ],
});

filterAtoms which determines the current user selection of all filters ( one of them is timeRange)

export const filterAtom = atom<any>({
   key: 'filterAtom',
   default: {time: [] },
});

Now in my code, whenever user select a date, i will update the timeRangeAtom value. I'm doing this by calling setTime of useRecoilState

  const [time, setTime] = useRecoilState(
    timeRangeAtom,
  );

Now my question is how can i also sync this change to my filterAtom.time whenever timeRangeAtom change ?

One way is i manually setFilterAtom by calling useRecoilState, but it seems quite tedious and duplicate code. So im not sure is there any other ways ?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can create derived state from other atoms using the selector API:

export const filterAtom = selector({
  key: 'filterAtom',
  get: ({get}) => {
    const timeRange = get(timeRangeAtom);
    return {time: [...timeRange]};
  },
});
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda