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

206
Visualizações
Why This API data is not rendering to React Page?

API for getting all kamForms data

router.get('/kam', (req, res) => {
  kamForm
    .find()
    .then((result) => {
      res.status(200).json({
        kamData: result,
      });
    })
    .catch((err) => {
      console.log(err);
      res.status(500).json({
        message: err,
      });
    });
});

And this is the API call and rendering part, when I inspect the browser all the data is showing in the console repeatedly. I will attach the ss of the console.

const PendingApplication = () => {
  const [data, setData] = useState([]);
  useEffect(() => {
    axios
      .get('http://localhost:5000/api/kam')
      .then((response) => {
        console.log(response);
        setData(response.data);
      })
      .catch((error) => {
        console.log(error);
      });
  });

  return (
    <div>
      <Table>
        <TableBody>
          {[data].map((item, index) => (
            <TableRow key={index}>
              <TableCell>{item.kcpname}</TableCell>
              <TableCell>{item.companyname}</TableCell>
              <TableCell>{item.ticketno}</TableCell>
              <TableCell>{item.totalemp}</TableCell>
              <TableCell>{item.kcpnid}</TableCell>
              <TableCell>{item.kcpcontact}</TableCell>
            </TableRow>
          ))}
        </TableBody>
      </Table>
    </div>
  );
};

This is how the data showing in the browser consloe

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Two problems.

First, you are causing an infinite loop in your useEffect hook because there is no dependancy array provided. This will run the effect on each render (which in turn sets the state once axios resolves, causing yet another render, which runs the effect, etc).

Second, you are mapping over an array [data].map, where data is already an array of results. You should probably mean to write data.map instead.

const PendingApplication = () => {
  const [data, setData] = useState([]);
  useEffect(() => {
    axios
      .get("https://rickandmortyapi.com/api/character")
      .then((response) => {
        console.log(response);
        setData(response.data.results);
      })
      .catch((error) => {
        console.log(error);
      });
  }, []);

  return (
    <div>
      <table>
        <tbody>
          {data.map((item, index) => (
            <tr key={index}>
              <td>{item.name}</td>
              <td>{item.status}</td>
              <td>{item.species}</td>
            </tr>
          ))}
        </tbody>
      </table>
    </div>
  );
};

Edit eager-sanderson-bwvj0

about 4 years ago · Juan Pablo Isaza Relatório

0

useEffect(() => {
    axios
        .get('http://localhost:5000/api/kam')
        .then((response) => {
            console.log(response);
            setData(response.data);
        })
        .catch((error) => {
            console.log(error);
        });
},[]);
return (
    <div>
        <Table>
            <TableBody>
                {data && data.map((item, index) => (
                    <TableRow key={index}>
                        <TableCell>{item.kcpname}</TableCell>
                        <TableCell>{item.companyname}</TableCell>
                        <TableCell>{item.ticketno}</TableCell>
                        <TableCell>{item.totalemp}</TableCell>
                        <TableCell>{item.kcpnid}</TableCell>
                        <TableCell>{item.kcpcontact}</TableCell>
                    </TableRow>
                ))}
            </TableBody>
        </Table>
    </div>
);

Use empty Square brackets like this at the end of the UseEffect. Everything will work fine. And also make sure data is not null or undefined before rendering.

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