Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to connect MySQL database to ReactJS app?

I have build a Todo App with create-react-app. The store I'm using is based on Local Storage(JS attribute of object window). Now I created a MySQL databases and want to connect to that database, so the state will show the values from database, and will be updated through actions.

I've tried to connect to db and output values through 'node' console using db.js. It works.

const mysql = require('mysql');

const con = mysql.createConnection({
  host: "localhost",
  user: "root",
  password: "root",
  database: 'root'
});

con.connect(function(err) {
  if (err) throw err;
  con.query("SELECT * FROM tasks", function (err, result, fields) {
    if (err) throw err;
    console.log(result);
  });
});

Is it possible to connect the state of app to database using this script?

like image 769
Iskender Berdiev Avatar asked Jan 23 '19 10:01

Iskender Berdiev


People also ask

Can we connect ReactJS with mysql database?

Yes. We are building an end-to-end project with React + Express + Node. js + MySQL. MySQL is an open-source relational database management system (RDBMS).

How does react app connect to database?

First, we create a react app, and then for backend maintenance, we create API in node. js and express. js which is running at a different port and our react app running at a different port. for connecting React to the database (MongoDB) we integrate through API.

How to connect react JS to a MySQL database?

Moreover, It’s not possible to directly connect React JS to a MySQL database. First, you need to connect the frontend with the backend and then the backend with the MySQL database. React is directly concerned with rendering and controlling UI elements.

How do I get data from a database in React Native?

In your React Native project (you can place it anywhere you want tho), create a file named ‘routes.js’. This will be our server (NodeJS), which we'll use to get data from the database. Yes, that's right. You're creating a (simple) NodeJS server! You can do a full CRUD if you want but that's not the point of this article! :p

How does a react app send data to express?

The React app then just sends HTTP requests to the Express server, which handles everything that needs verification and authorization before even touching the data. If you search the internet for any configuration of a fullstack architecture using React and MySQL, you'll find similar results to what I mentioned.

What is react JS?

React.js is a library for web development. It was created by Facebook to simplify the process of creating sophisticated and responsive user interfaces. UI components are created with React.js via a virtual document object model (DOM model). The virtual DOM does not require a response from the browser, so the code is displayed faster.


2 Answers

You can't connect them directly.

JavaScript running in a web browser cannot speak the MySQL protocol (nor can it make raw network connections that would be needed to write an implementation in JS).

Instead, create a web service (in the programming language of your choice, which could be JavaScript running on Node.js (e.g. the code you have already + Express.js + some glue)) and use Ajax to communicate with it.

like image 85
Quentin Avatar answered Oct 17 '22 21:10

Quentin


The general solution for a question like this is the following framework:

  • Back-end (Node.js, Express, Database connection including authorization)
  • Front-end (React(, Redux to manage state))

If you then launch the React app, it should populate its state based on data retrieved from the database, which is a process to which you can add authorization (make retrievable data depend on the role/status of the user).

In the back-end you can define functions that take in a certain subset of parameters, which performs database actions, to which you can add business rules for your application. The React app then just sends HTTP requests to the Express server, which handles everything that needs verification and authorization before even touching the data.

If you search the internet for any configuration of a fullstack architecture using React and MySQL, you'll find similar results to what I mentioned.

like image 17
Thomas Brok Avatar answered Oct 17 '22 20:10

Thomas Brok