DEV Community


Posted on

Simple React JS and MySQL Integration -- CRUD App (Frontend)

So, here we are creating the frontend part of our React JS and MySQL Integration -- CRUD App and we will begin by creating a new react app by typing in npx create-react-app myappname in the terminal.

This will create a new react app with the default template and now we need to do a little cleanup i-e remove the test files and also remove everything from App.js file and write our own code.

In the App.js file, I will create a div with className="navbar" and another div with className="links". In the links div we'll define two routes where we want our app to navigate. First is the default route or homepage which is defined as:

 <a href="/">Main Page</a>
Enter fullscreen mode Exit fullscreen mode

The other route is going to be /createpost route which we will define as:

  <a href="/createpost">Create Post</a>
Enter fullscreen mode Exit fullscreen mode

In order to navigate to the above mentioned links, we need to import react-router in our App.js file as follows:

import {BrowserRouter as Router, Route} from "react-router-dom"
Enter fullscreen mode Exit fullscreen mode

Also, we need to define the router paths in a tag.
The complete structure of our App.js file will look like this now:

import React from 'react'
import {BrowserRouter as Router, Route} from "react-router-dom";
import './App.css'
import CreatePost from './pages/CreatePost';
import MainPage from './pages/MainPage';
import Post from './pages/Post'

const App = () => {
  return (
     <div className="navbar">
      <div className="links"> 
      <a href="/">Main Page</a>
      <a href="/createpost">Create Post</a>
   <Route path="/" exact render={(props) => <MainPage />} />
   <Route path="/createpost" render={(props)=> <CreatePost />} />
   <Route path="/post/:postId" render={(props)=> <Post />}/>
export default App;
Enter fullscreen mode Exit fullscreen mode

Now, we'll define the Main Page route in pages folder in the src folder --> MainPage.js file. Here, we'll first display all the blog posts (since this is a simple blog app) by using map method over the already created Posts and using the Axios library to get the posts and also like the posts. To store the posts list, we will take help from the useState hook of react and display the posts using the useEffect hook, the overall code of MainPage.js file looks like this:

import React,{useState,useEffect} from 'react'
import Axios from 'axios'
import {useHistory} from 'react-router-dom'
import '../App.css'

function MainPage() {

const [postList,setPostList]=useState([]);

let history = useHistory();


const LikePost = (id) => {`http://localhost:3002/api/like/${id}`).then((response)=>{
alert("you liked a post")
return (
    <div className="MainPage">
     <div className="PostContainer">
         return (
          <div className="Post" >
           <h1 className="post-title" onClick={()=>(history.push(`/post/${}`))}>{val.title}</h1>
            <p>{val.post_text.length > 300 ? val.post_text.substring(0,300)+ " ..." : val.post_text}</p>
<button className="like_btn" onClick={(() => LikePost(}>Like</button>

           <h5>Likes: {val.likes}</h5>
           )  })}  

export default MainPage
Enter fullscreen mode Exit fullscreen mode

Now, we move on to creating a post, this we will do in the CreatePost.js file in the pages folder.Since, we have three input fields i-e userName, title and text so we will define three useState hooks to store the data, the code structure will look like this:

import React,{useState, useEffect} from 'react';
import Axios from 'axios'
import '../App.css'

function CreatePost() {

const [userName,setUserName] = useState("");
const [title,setTitle] = useState("");
const [text,setText] = useState("");

const submitPost = () => {'http://localhost:3002/api/create', {userName: userName, title: title, text:text})

    return (
        <div className="CreatePost">
            <div className="uploadPost">
                <label>Username: </label>
                <input type="text" onChange={(e)=> {
                <label>Title: </label>
                <input type="text" onChange={(e)=>{
       <label>Post Text</label>
       <textarea onChange={(e)=>{
<button onClick={submitPost}>Submit Post</button>

export default CreatePost
Enter fullscreen mode Exit fullscreen mode

Here, we come to the conclusion of our CRUD App, I know it's been a little long but I am sure it will teach you a great deal about React and will help you diversify your knowledge.
Below is the link of github repo where you can find complete code:

Also, below is the link where you can find the backend part of this app:

Happy coding guys and have a great day!

Top comments (1)

sahing profile image
Sahin Ahmed

Thanks a lot Maadam for the helpful content