Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

215
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda