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

214
Views
Passing Async State to Next.js Component via Prop

I'm fetching WordPress posts asynchronously via getStaticProps()...

export async function getStaticProps({ params, preview = false, previewData }) {
    const data = await getPostsByCategory(params.slug, preview, previewData)
    return {
        props: {
            preview,
            posts: data?.posts
        },
    }
}

... and passing them to useState:

const [filteredArticles, setFilteredArticles] = useState(posts?.edges)

Then, I pass the state to a component:

router.isFallback ? (
    // If we're still fetching data...
    <div>Loading…</div>
) : (
    <ArticleGrid myArticles={filteredArticles} />

This is necessary because another component will setFilteredArticles with a filter function.

But when we are passing the state to ArticlesGrid, the data is not ready when the component loads. This is confusing to me since we passing the state within a router.isFallback condition.

Even if we set state within useEffect...

const [filteredArticles, setFilteredArticles] = useState()
useEffect(() => {
    setFilteredArticles(posts)
}, [posts?.edges])

... the data arrives too late for the component.

I'm new to Next.js. I can probably hack my way through this, but I assume there's an elegant solution.

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

0

Let's look at some useEffect examples:

useEffect(() => {
  console.log("Hello there");
});

This useEffect is executed after the first render and on each subsequent rerender.

useEffect(() => {
  console.log("Hello there once");
}, []);

This useEffect is executed only once, after the first render.

useEffect(() => {
  console.log("Hello there when id changes");
}, [props.id]);

This useEffect is executed after the first render, every time props.id changes.

Some possible solutions to your problem:

No articles case

You probably want to treat this case in your ArticleGrid component anyway, in order to prevent any potential errors.

In ArticleGrid.js:

const {myArticles} = props;

if(!myArticles) {
    return (<div>Your custom no data component... </div>);
}

// normal logic
return ...

Alternatively, you could also treat this case in the parent component:

{
  router.isFallback ? (
    // If we're still fetching data...
    <div>Loading…</div>
  ) : (
    <>
      {
        filteredArticles
          ? <ArticleGrid myArticles={filteredArticles} />
          : <div>Your custom no data component... </div>
      }
    </>
  )
}

Use initial props

Send the initial props in case the filteres haven't been set:

const myArticles = filteredArticles || posts?.edges;

and then:

<ArticleGrid myArticles={myArticles} />
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!