I have a simple Flask backend and I'm trying to fetch json data from it in a simple React frontend. However when I run the React code I get an error: src/App.js Line 8:57: 'data' is not defined no-undef. From the examples I've seen it seems data should be accessible. I'm not trying to do anything complicated with states or effects, I'd like as a first step to be able to simply fetch the Flask json data and print it out in the frontend.
My Flask backend is hello.py:
from flask import Flask
app = Flask("__name__")
@app.route('/hello/')
def hello():
return {'1': 'hello'}
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8000)
React files. App.js:
import React from 'react';
let url = 'http://localhost:8000/hello
function App() {
fetch(url).then(res => res.json()).then(console.log(data))
export default App;
index.js:
import React from "react";
import ReactDOM from "react-dom";
import App from "./App";
ReactDOM.render(<App />, document.getElementById("root))
As mentioned in my comment you are missing to define the data variable. It's like in the then Block where you access the res. So maybe it works if you write something like:
function App() {
fetch(url).then(res => res.json()).then(data => console.log(data))