How do i fetch data from my MySql database as json and then use it in react js…? actually i’m new to React and MySql so i just don’t know much about it.. Please help
How do i fetch data from my MySql database as json and then use it in react js…? actually i’m new to React and MySql so i just don’t know much about it.. Please help
7
Answers
first, you have to solve the console error of key of react, give the key wherever you
React is not allowed to connect to MySql directly. We need NodeJs to start a back-end server which supporting HTTP API.
You can watch this video, and try to create your demo.
React Node.js MySQL CRUD Tutorial for Beginners
I agree with YinPeng.Wei’s idea for setting up a backend server to retrieve data from database. The majority programming language for backend development have packages or libraries to connect with Database.
A similar question was asked years ago about How to connect to SQL Server database from JavaScript in the browser?. The answer is you could do that but it is high discouraged.
The general idea is to create a simple backend server which responds to your React frontend requests. Backend retrieves data from MySQL via sql query, then serialize searched data into JSON format and gives back to your frontend.
Not sure which type of programming language would you choose for backend development. I would do either one of Python, C#, Java for a quick demo.
Typically, you are using React to connect to another app (backend) via an API e.g. REST, THAT backend is the one who connect to the database and passes the data to your React app.
Your react app doesn’t even know there is a database, all he knows is the backend that’s feeding him. without him he’s just a pretty looking dead app.
now from your backend, you have two ways, using languages built-in driver to connect to your database directly (hand writing SQL statements), Or you may use an ORM, just google "js orm" and you will find many.
As a start, you need to learn more about creating a simple REST API with whatever language you choose, and then simply use
fetch("http://example.com/whatever").then(res=> JSON.parse(res)).then(res=> console.log(res))
the code above should show you whatever you see on the screen when you actually visit the URL inside
fetch()
from the browser, just text.REST itself is just a way to use HTTP that people like.
the browser itself is just another client (like your React app), if he can do it so does your app.
The backend has some URLs called apis which send some requests as
get
orpost
. The react catches that request via fetch or axios and update its components states. People usefetch
oraxios
library to fetch that request and to handle state management libraries likeredux
if it is very large and uses built-in react API likereducer
hooks and some others.Below is an example of how to use fetch
create a connection to your MySQL database in Serverside (Node js)
Add an API route to fetch the data from the MySQL database
fetch data in your react component
I hope it should work
You need a backend to communicate from React to MySQL. This backend need an API which you can export data directly from MySQL based on a command. Basically, if you wanted to make a query you would need to do it via a HTTP API. It could look something like this:
The only thing you need now is to send a HTTP request/fetch like this to get the response of the sql query.
PS I would **not advise to use this solution directly, as it is not safe, since it allows anybody to execute any query in your database. But it shows you what is possible.