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?
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).
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.
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.
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
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.
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.
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.
The general solution for a question like this is the following framework:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With