Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

REACT AXIOS Parsing Json

I am new to react, so if I am doing anything outside of the problem wrong please tell me also.

I’m trying to map my json response into a table, I can collect the data into an object array, but I am receiving this error :
enter image description here

Here is the components code:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

import axios from "axios";

function FilmTableRows(props) {
  const dataFormat = props.dataFormat;
  const [data, setData] = useState([]);
  const baseURL = "http://localhost:8080/FilmRestful/filmapi";

  const getJson = () => {
    let config = {
      headers: {
        "data-type": "json",
        "Content-type": "application/json",
      },
    };
    axios
      .get(baseURL, config)
      .then((res) => {
        const resData = res.data;
        setData(resData);
      })
      .catch((err) => {});
  };

  switch (dataFormat.value) {
    case "json":
      getJson();
      console.log(data);
      break;
    case "xml":
      getXML();
      console.log(data);
      break;
    default:
      getString();
      console.log(data);
  }

  const child = data.map((el) => {
    return (
      <tr key={el.id}>
        <td>{el.title}</td>
        <td>{el.year}</td>
        <td>{el.director}</td>
        <td>{el.stars}</td>
        <td>{el.review}</td>
      </tr>
    );
  });

  return <>{data && data.length > 0 && { child }}</>;
}

export default FilmTableRows;

>Solution :

The error is caused because you wrap child with {} when you render it, which turns it into an object with an array variable. Try removing them like so:

return <>{data && data.length > 0 && child }</>;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading