Skip to content

What's the best way to cache a selectors return value until a certain timeout #1728

Answered by richardanaya
richardanaya asked this question in Q&A
Discussion options

You must be logged in to vote

I ended up doing something like this! I did a selector. Thanks for the info on refreshing just clearing the cache and not immediately evaluating.

import { differenceInMilliseconds, secondsToMilliseconds } from "date-fns";
import { useEffect } from "react";
import { useSetRecoilState } from "recoil";
import { fetchPokemon } from "../client/pokemonClient";
import { pokemonState } from "../state/pokemon";
let lastRetrieved: Date | undefined;
export function usePokemon() {
  const setPokemon = useSetRecoilState(pokemonState);

  useEffect(() => {
    loadPokemon();
  }, []);

  if (lastRetrieved) {
    const difference = differenceInMilliseconds(new Date(), lastRetrieved);
    if (difference > 

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by richardanaya
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 #1725 on April 11, 2022 18:50.