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

280
Views
How to update redux store with react suspense

In following code, Sample uses a async function to get some data. I will be using that data to update the Redux store so any component can access the username within the application.

const resource = fetchData();

function Sample() {
    // throws a promise when retrieving data
    const name = resource.read();

    const dispatch = useDispatch();
    dispatch(setUsername(name));

    const username = useSelector((state) => state.username);

    return <div>User: {username}</div>;
}

<Suspense fallback="loading....">
    <Sample />
</Suspense>

Here, lets assume there is no way my application can proceed without the username. Well, <Suspense> at parent level achieves that up until data are fetched from the resource. However, there is a slight gap from Redux event dispatch to <Sample> component re-render where is displays User: instead of loading.... (event don't force the re-render immediately so username is empty). So I will see following content in the web page in the same order

loading.... --> User: --> User: Srinesh

So my requirement is to show loading.... until store is updated. On the web page, I'm expecting to see,

loading.... --> User: Srinesh

Is there a way to achieve this without using another condition at <Sample> component level to show loading.... if the username is null?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The first issue is that dispatching in the middle of component rendering logic is a side effect, and you must never do that.

A safer place to put that would be in a useLayoutEffect hook, which will dispatch as soon as the component is done rendering, but force a synchronous re-render before the browser has a chance to paint. That way you won't see the flash.

about 4 years ago · Juan Pablo Isaza Report
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!