Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

92
Views
React Recoil persist state between routes

I want to use Recoil to share a state between to React Router Routes. My setup looks something like this:

ReactDOM.render(
  <React.StrictMode>
    <RecoilRoot>
      <BrowserRouter>
        <Routes>
          <Route path="/" element={<App />} />
          <Route path="/checkin" element={<Checkin />} />
          <Route path="/checkin/confirm" element={<ConfirmCheckin />} />
        </Routes>
      </BrowserRouter>
    </RecoilRoot>
  </React.StrictMode>,
  document.getElementById("root")
);

My atom setup looks like this:

const checkinItemsState = atom<InventoryItem[]>({
  key: "checkinItemsState",
  default: [],
});

In both components i use useRecoilState(checkinItemsState)

Now when i open localhost:3000/checkin it shows my state and i can modify it. When i then navigate to localhost:3000/checkin/confirm my state is just an empty array.

What am I missing here? is there something i misunderstand?

So fare I've tried to wrap my Routes with a component like:

const Index: React.FC<{ children: ReactNode }> = ({ children }) => {
  useRecoilState(checkinItemsState);

  return (
    <>
      <span>Hello World</span>
      {children}
    </>
  );
};

I guessed maybe it was because the state was nowhere referenced when moving between urls. This does not work.

Any help would be appreciated.

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!