Skip to content

Best practice for guaranteeing query refresh on new page loads #1880

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

You must be logged in to vote

I ended up going with something like this;

const RecoilUpdater: React.FC = () => {
  const pathname = useLocation().pathname;
  // We only want to monitor if the root page changes
  const newCurrentPage = pathname.split("/")[1];
  const previousCurrentPage = useRecoilValue(currentPageState);
  const setCurrentPage = useSetRecoilState(currentPageState);

  useEffect(() => {
    if (newCurrentPage && previousCurrentPage !== newCurrentPage) {
      setCurrentPage(newCurrentPage);
    }
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [pathname]);
  return null;
};

and have my recoil states that I want to be refreshed be dependent on this

Using a component to leverage the use…

Replies: 3 comments

Comment options

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

You must be logged in to vote
0 replies
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