0

I already asked this question here, but did not receive an answer to it, this time I transferred everything to the sandbox, so that it is easier to understand the essence of the problem

How to get local JSON (fetch)? I do a service, then I call the class instance This is not how it works:

import React from "react";
import "./styles.css";

export default function App() {
  const onFetch = async () => {
    const req = await fetch("./data.json");
    return req.json();
  };

  const onMap = () => {
    onFetch().then(data => console.log(data));
  };

  return <div className="App">{onMap()}</div>;
}

Unexpected token < in JSON at position 0 https://codesandbox.io/s/silly-montalcini-2z146

  • You have to put the URL to an actual JSON file and not a file where the first character is `<` (which is presumably an HTML document). – Quentin Feb 19 '20 at 11:00
  • Where is the json, show us the structure. – Grumpy Feb 19 '20 at 11:01
  • You can see that `view-source:https://2z146.csb.app/data.json` is the HTML document that bootstraps your react app. – Quentin Feb 19 '20 at 11:01
  • It's also worth mentioning your code above even if you get your `json` will not work anyway.. `onFetch` is async, and it will remain async even if you place inside a function called `onMap()`.. – Keith Feb 19 '20 at 11:08
  • https://i.imgur.com/GnFmJva.png?1 –  Feb 19 '20 at 11:15

0 Answers0