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

129
Visualizações
How to render based on AJAX result in react js?

Just want to render movie cards based on results that come from ajax call.

Currently, the movie cards components are rendered based on that hard code array named list. I just want to make it dynamic and replace it with my ajax data.

const getlist = async () => {
  const res = await fetch('http://localhost:3001/customize');
  const data = await response.json();
  getlist();
};

export default function Index() {

  const list = ['dexter', 'bb', 'got'];

  return (
    <>
      <main className={parentstyle.main_container}>
        <NavBar />
        <div className={style.searchbar_container}>
          <SearchBar />
        </div>
        <div className={style.card_container}>
          {test.map((element, i) => {
            return <MovieCard movieName={element} key={i} />;
          })}
        </div>
      </main>
    </>
  );
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Use the useState hook to set up your component state (the list) and fetch data in a useEffect hook...

The Effect Hook lets you perform side effects in function components:

Data fetching, setting up a subscription, and manually changing the DOM in React components are all examples of side effects. Whether or not you’re used to calling these operations “side effects” (or just “effects”), you’ve likely performed them in your components before.

import { useEffect, useState } from "react"

const getlist = async () => {
  const res = await fetch("http://localhost:3001/customize")
  if (!res.ok) {
    throw new Error(`${res.status}: ${await res.text()}`)
  }
  return res.json()
}

const Index = () => {

  const [ list, setList ] = useState([]) // start with an empty array

  useEffect(() => {
    getList()
      .then(setList)
      .catch(console.error)
  }, []) // empty dependencies array, this runs only once

  return (
    // ...

    {list.map((element, i) => (
      <MovieCard movieName={element} key={i} />
    ))}

    // ...
  )
}

export default Index
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