Fetch data from a local json file in React

PHOTO EMBED

Sat May 07 2022 15:41:30 GMT+0000 (UTC)

Saved by @jamzlego #react.js

  const [data, setData] = useState([]);

  //Fetch data from a local json file.
  const getData = () => {
    fetch("data.json", {
      headers: {
        "Content-Type": "application/json",
        Accept: "application/json",
      },
    })
      .then(function (response) {
        console.log(response);
        return response.json();
      })
      .then(function (myJson) {
        console.log(myJson);
        setData(myJson);
      });
  };

  useEffect(() => {
    getData();
  }, []);
content_copyCOPY