How to Connect ReactJS with flask API ?
Last Updated :
10 Oct, 2024
Connecting React with Flask API allows us to create a full-stack web application with React handling the user interface at frontend and Flask serving the data in backend. Here React makes the HTTP request to interact with the flask API to perform operations with the database.
Approach
To connect React JS with Flask API we will be using the data fetching methods and libraries like fetch and axios. Build the backend API using the flask and make HTTP requests from React frontend using JavaScript fetch method for efficient API communication.
Let’s see a Step-by-step guide to connect Flask API with React JS to show the data on the web page.
Steps to Connect React with Flask API
Note : Make sure to make 2 separate folders for clean understanding, one for the Flask backend and one for the React frontend. The project structure should look like

Step 1: SetUp the Flask Server
Make a folder named backend and file server.js with the following command:
mkdir backend
cd backend
touch server.py
Build a basic flask server. Write down the following code in server.py file.
Example: This example craete a backend server that provides json data when http request is made at /data route.
Python
# Filename - server.py
# Import flask and datetime module for showing date and time
from flask import Flask
import datetime
x = datetime.datetime.now()
# Initializing flask app
app = Flask(__name__)
# Route for seeing a data
@app.route('/data')
def get_time():
# Returning an api for showing in reactjs
return {
'Name':"geek",
"Age":"22",
"Date":x,
"programming":"python"
}
# Running app
if __name__ == '__main__':
app.run(debug=True)
Step to run the application: Use the following command to run the server:
python server.py
Output: This output will be visible on the http://localhost:5000/ on the browser window..

Step 2: Create React Application
Make a react project using the following command:
yarn create react-project frontend
// OR
npx create-react-app frontend
Move After creating the app, move into the app using the following command:
cd frontend
After that open package.json and add the proxy.
"proxy":"http://localhost:5000/"
Now, we provide the proxy in react package.json file because we need to access the flask URL in order to get the API from our react app. In general what proxy does is, when we request into the javascript web server which serves the react frontend will automatically be redirected to the proxy key. In this case, it will be our flask server.Â

Step 3: Fetching Data From the API
For fetching the API useState and useEffect hooks are used in react app.Â
- useState: It is used for setting a data from the API and providing into the jsx for showing the data.
- useEffect: It is used for rendering a fetch method on a single reload.
Example: This example uses the react hooks to fetche data from a Flask API and displays the name, age, date, and programming language on the webpage.
JavaScript
// Filename - App.js
// Importing modules
import React, { useState, useEffect } from "react";
import "./App.css";
function App() {
// usestate for setting a javascript
// object for storing and using data
const [data, setdata] = useState({
name: "",
age: 0,
date: "",
programming: "",
});
// Using useEffect for single rendering
useEffect(() => {
// Using fetch to fetch the api from
// flask server it will be redirected to proxy
fetch("/data").then((res) =>
res.json().then((data) => {
// Setting a data from api
setdata({
name: data.Name,
age: data.Age,
date: data.Date,
programming: data.programming,
});
})
);
}, []);
return (
<div className="App">
<header className="App-header">
<h1>React and flask</h1>
{/* Calling a data from setdata for showing */}
<p>{data.name}</p>
<p>{data.age}</p>
<p>{data.date}</p>
<p>{data.programming}</p>
</header>
</div>
);
}
export default App;
Step to run the application: Use the following command to run the server:
npm start
// OR
yarn start
Output: This output will be visible on the http://localhost:3000/ on the browser window.

Similar Reads
How to connect ReactJS with flask API ?
Connecting React with Flask API allows us to create a full-stack web application with React handling the user interface at frontend and Flask serving the data in backend. Here React makes the HTTP request to interact with the flask API to perform operations with the database. ApproachTo connect Reac
4 min read
How to Connect Django with Reactjs ?
Connecting Django with React is a common approach for building full-stack applications. Django is used to manage the backend, database, APIs and React handles the User Interface on frontend. Prerequisites:A development machine with any OS (Linux/Windows/Mac).Python 3 installed.Node.js installed (ver
9 min read
How To Connect Node with React?
To connect Node with React, we use React for the frontend (what users see) and Node.js for the backend (where the server logic lives). The frontend sends requests to the backend, and the backend responds with data or actions. There are many ways to connect React with Node.js, like using Axios or Fet
4 min read
How To Connect MongoDB with ReactJS?
MongoDB is a popular NoSQL database known for its scalability, flexibility, and high performance. When building modern web applications with ReactJS, itâs common to connect your frontend with a backend server that interacts with a database like MongoDB. PrerequisiteReact JSNode JSMongo DBApproach to
5 min read
How to connect ReactJS with MetaMask ?
To Connect React JS with MetaMask is important while making Web 3 applications, It will be done with Meta mask wallet which is the most used browser tool for sending and receiving signed transactions . MetaMask is a crypto wallet and a gateway to blockchain DAPP. It is used to connect our app to web
3 min read
How To Create a Website in ReactJS?
ReactJS is one of the most popular JavaScript libraries for building user interfaces. It allows you to create dynamic, reusable UI components and efficiently manage state and events. In this article, we'll walk through the steps to create a basic website using ReactJS. PrerequisitesNPM & Node.js
5 min read
What is the React Context API?
In the React ecosystem, as your application grows, passing data down through component hierarchies can become cumbersome. This is where the Context API steps in, providing a centralized way to manage state across components. Table of Content What is the Context API?How Context API Works:Steps to Cre
4 min read
How to Change Port in Flask app
In this article, we will learn to change the port of a Flask application. The default port for the Flask application is 5000. So we can access our application at the below URL. http://127.0.0.1:5000/ We may want to change the port may be because the default port is already occupied. To do that we ju
1 min read
How to Convert CSV to JSON in ReactJS ?
Dealing with data in various formats is a common task in web development. CSV (Comma Separated Values) and JSON (JavaScript Object Notation) are two widely used formats for storing and transmitting data. Converting CSV to JSON is often required when working with data in ReactJS applications. Approac
4 min read
How to Connect ReactJS as a Front-end with PHP as a Back-end ?
Connecting the front end of React with the PHP backend is a classic approach to building a web application with the front end for the user interface and the back end or server side for handling databases, programming logic, and other operations. In this article, weâll discuss the steps required to c
4 min read