Welcome to part two of our tutorial on building a MERN stack app from scratch. In our previous post, “MERN stack A to Z: Part 1,” we completed the back-end portion with Node.js, Express.js, and MongoDB and created our Rest APIs. Here, we are going to use those APIs.

In this part, we will work with React to build out the front-end portion of our app. React is a JavaScript library for building user interfaces. It is maintained by Facebook and a community of individual developers and other companies.

Prerequisites and project requirements

We will use create-react-app to generate our initial file setup. CRA is a comfortable environment for learning React and is the best way to start building applications in React. It offers a modern build setup with no configuration.

We’ll also be using webpack and Babel to bundle our modules and compile our JavaScript, respectively. Don’t know webpack or Babel well? No problem!

You don’t need to install or configure tools like webpack or Babel. They are preconfigured and hidden so that you can focus on the code. Just create a project, and you’re good to go.

You’ll also need any version of Node greater than 8.10 and any version of npm greater than 5.6 installed on your local development machine.

Setting up create-react-app

Set any directory using a terminal where you want to keep all the files of this project and run the following command to get the initial setup file:

$ npx create-react-app my-app

You can replace my-app with whatever you’d like to use as your project name. For example, my project name is mern_a_to_z_client , and my command is:

$ npx create-react-app mern_a_to_z_client

NOTE: The project name must be in lowercase letters.

If everything goes right, then you will see something like the following image, where you will find some instructions along with the commands.

Before using any built-in command, we need to go inside the project folder.

$ cd mern_a_to_z_client

Now that we are in the project directory, we can use those available commands. If you’re using Yarn:

$ yarn start

Or, if using npm:

$ npm start

To run the app in development mode, you can use any of the above commands, and you will see the following message in your terminal.

Now open http://localhost:3000 to view it in the browser. This page will automatically reload if you make changes to the code.

Initial project structure

Inside the project directory, our initial file structure should look like this:

Adding Bootstrap and Font Awesome

We have got our initial setup file for the front-end part. Now we can start integrating our back end with our front end. Before that, though, I want to add Bootstrap and Font Awesome’s CDN to our project.

Open the file called index.html , which is in the public folder mern_a_to_z_client/public/index.html , and replace everything with the following code:

<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8" /> <link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <meta name="theme-color" content="#000000" /> <!-- manifest.json provides metadata used when your web app is installed on a user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/ --> <link rel="manifest" href="%PUBLIC_URL%/manifest.json" /> <!-- Notice the use of %PUBLIC_URL% in the tags above. It will be replaced with the URL of the `public` folder during the build. Only files inside the `public` folder can be referenced from the HTML. Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will work correctly both with client-side routing and a non-root public URL. Learn how to configure a non-root public URL by running `npm run build`. --> <!-- bootstrap css cdn --> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous"> <!-- fontawesome cdn --> <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.2.0/css/all.css" integrity="sha384-hWVjflwFxL6sNzntih27bfxkr27PmbbK/iSvJ+a4+0owXq79v+lsFkW54bOGbiDQ" crossorigin="anonymous"> <title>MERN A to Z</title> </head> <body> <noscript>You need to enable JavaScript to run this app.</noscript> <div id="root"></div> <!-- This HTML file is a template. If you open it directly in the browser, you will see an empty page. You can add webfonts, meta tags, or analytics to this file. The build step will place the bundled scripts into the <body> tag. To begin the development, run `npm start` or `yarn start`. To create a production bundle, use `npm run build` or `yarn build`. --> <!-- bootstrap JS cdn --> <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script> </body> </html>

Front-end tasks and features

We will work with five different features:

Add, create or save a new book Show all the books we have stored in the database Show a single book Update a book Delete a book

Dependencies packages installation

Now, use the following command to add some necessary dependencies:

$ npm install --save react-router-dom $ npm install --save axios

Why Axios?

Axios is a lightweight HTTP client based similar to a Fetch API. Axios is a promise-based async/await library for readable asynchronous code. We can easily integrate with React, and it is effortless to use in any front-end framework.

We will call our APIs through Axios.

Package.json file

At this point, our package.json file should be similar to this; versions can be similar or different:

// MERN_A_to_Z_Client - package.json { "name": "mern_a_to_z_client", "version": "0.1.0", "private": true, "dependencies": { "axios": "^0.19.0", "react": "^16.8.6", "react-dom": "^16.8.6", "react-router-dom": "^5.0.1", "react-scripts": "3.0.1" }, "scripts": { "start": "react-scripts start", "build": "react-scripts build", "test": "react-scripts test", "eject": "react-scripts eject" }, "eslintConfig": { "extends": "react-app" }, "browserslist": { "production": [ ">0.2%", "not dead", "not op_mini all" ], "development": [ "last 1 chrome version", "last 1 firefox version", "last 1 safari version" ] } }

Creating the component file

Inside the src folder ( mern_a_to_z_client/src/ ), create another folder called components , and inside it, create five different files:

CreateBook.js ShowBookList.js BookCard.js ShowBookDetails.js UpdateBookInfo.js

We will work with these five files a bit later.

Setup route

Open the folder called App.js inside the src folder ( mern_a_to_z_client/src/App.js ), and replace it with the following code:

import React, { Component } from 'react'; import { BrowserRouter as Router, Route } from 'react-router-dom'; import './App.css'; import CreateBook from './components/CreateBook'; import ShowBookList from './components/ShowBookList'; import ShowBookDetails from './components/ShowBookDetails'; import UpdateBookInfo from './components/UpdateBookInfo'; class App extends Component { render() { return ( <Router> <div> <Route exact path='/' component={ShowBookList} /> <Route path='/create-book' component={CreateBook} /> <Route path='/edit-book/:id' component={UpdateBookInfo} /> <Route path='/show-book/:id' component={ShowBookDetails} /> </div> </Router> ); } } export default App;

Here, we define all the routes. For a specific path definition, its corresponding component will be rendered. We have not implemented these files/components yet — just completed the path setup.

Update the CSS file

Update a CSS file called App.css in the src folder with the following code:

.App { text-align: center; } .App-logo { animation: App-logo-spin infinite 20s linear; height: 40vmin; pointer-events: none; } .App-header { background-color: #282c34; min-height: 100vh; display: flex; flex-direction: column; align-items: center; justify-content: center; font-size: calc(10px + 2vmin); color: white; } .App-link { color: #61dafb; } @keyframes App-logo-spin { from { transform: rotate(0deg); } to { transform: rotate(360deg); } } .CreateBook { background-color: #2c3e50; min-height: 100vh; color: white; } .ShowBookDetails { background-color: #2c3e50; min-height: 100vh; color: white; } .UpdateBookInfo { background-color: #2c3e50; min-height: 100vh; color: white; } .ShowBookList { background-color: #2c3e50; height: 100%; width: 100%; min-height: 100vh; min-width: 100px; color: white; } /* BookList Styles */ .list { display: grid; margin: 20px 0 50px 0; grid-template-columns: repeat(4, 1fr); grid-auto-rows: 1fr; grid-gap: 2em; } .card-container { width: 250px; border: 1px solid rgba(0,0,.125); margin: 0 auto; border-radius: 5px; overflow: hidden; } .desc { height: 130px; padding: 10px; } .desc h2 { font-size: 1em; font-weight: 400; } .desc h3, p { font-weight: 300; } .desc h3 { color: #6c757d; font-size: 1em; padding: 10px 0 10px 0; }

Adding our feature components

Create a new book

Our CreateBook.js file is responsible for adding, creating, or saving a new book or a book’s info. So, update CreateBook.js with the following code:

import React, { Component } from 'react'; import { Link } from 'react-router-dom'; import '../App.css'; import axios from 'axios'; class CreateBook extends Component { constructor() { super(); this.state = { title: '', isbn:'', author:'', description:'', published_date:'', publisher:'' }; } onChange = e => { this.setState({ [e.target.name]: e.target.value }); }; onSubmit = e => { e.preventDefault(); const data = { title: this.state.title, isbn: this.state.isbn, author: this.state.author, description: this.state.description, published_date: this.state.published_date, publisher: this.state.publisher }; axios .post('http://localhost:8082/api/books', data) .then(res => { this.setState({ title: '', isbn:'', author:'', description:'', published_date:'', publisher:'' }) this.props.history.push('/'); }) .catch(err => { console.log("Error in CreateBook!"); }) }; render() { return ( <div className="CreateBook"> <div className="container"> <div className="row"> <div className="col-md-8 m-auto"> <br /> <Link to="/" className="btn btn-outline-warning float-left"> Show BooK List </Link> </div> <div className="col-md-8 m-auto"> <h1 className="display-4 text-center">Add Book</h1> <p className="lead text-center"> Create new book </p> <form noValidate onSubmit={this.onSubmit}> <div className='form-group'> <input type='text' placeholder='Title of the Book' name='title' className='form-control' value={this.state.title} onChange={this.onChange} /> </div> <br /> <div className='form-group'> <input type='text' placeholder='ISBN' name='isbn' className='form-control' value={this.state.isbn} onChange={this.onChange} /> </div> <div className='form-group'> <input type='text' placeholder='Author' name='author' className='form-control' value={this.state.author} onChange={this.onChange} /> </div> <div className='form-group'> <input type='text' placeholder='Describe this book' name='description' className='form-control' value={this.state.description} onChange={this.onChange} /> </div> <div className='form-group'> <input type='date' placeholder='published_date' name='published_date' className='form-control' value={this.state.published_date} onChange={this.onChange} /> </div> <div className='form-group'> <input type='text' placeholder='Publisher of this Book' name='publisher' className='form-control' value={this.state.publisher} onChange={this.onChange} /> </div> <input type="submit" className="btn btn-outline-warning btn-block mt-4" /> </form> </div> </div> </div> </div> ); } } export default CreateBook;

Show all books

The ShowBookList.js component will be responsible for showing all the books we already have stored in our database. Update ShowBookList.js with this code:

import React, { Component } from 'react'; import '../App.css'; import axios from 'axios'; import { Link } from 'react-router-dom'; import BookCard from './BookCard'; class ShowBookList extends Component { constructor(props) { super(props); this.state = { books: [] }; } componentDidMount() { axios .get('http://localhost:8082/api/books') .then(res => { this.setState({ books: res.data }) }) .catch(err =>{ console.log('Error from ShowBookList'); }) }; render() { const books = this.state.books; console.log("PrintBook: " + books); let bookList; if(!books) { bookList = "there is no book record!"; } else { bookList = books.map((book, k) => <BookCard book={book} key={k} /> ); } return ( <div className="ShowBookList"> <div className="container"> <div className="row"> <div className="col-md-12"> <br /> <h2 className="display-4 text-center">Books List</h2> </div> <div className="col-md-11"> <Link to="/create-book" className="btn btn-outline-warning float-right"> + Add New Book </Link> <br /> <br /> <hr /> </div> </div> <div className="list"> {bookList} </div> </div> </div> ); } } export default ShowBookList;

Card for each book

Here we use a functional component called BookCard.js , which takes a book’s info from ShowBookList.js and makes a card for each book. Write the following code to update your BookCard.js file:

import React from 'react'; import { Link } from 'react-router-dom'; import '../App.css'; const BookCard = (props) => { const book = props.book; return( <div className="card-container"> <img src="https://commapress.co.uk/books/the-book-of-cairo/cairo-provisional-v3/image%2Fspan3" alt="" /> <div className="desc"> <h2> <Link to={`/show-book/${book._id}`}> { book.title } </Link> </h2> <h3>{book.author}</h3> <p>{book.description}</p> </div> </div> ) }; export default BookCard;

NOTE: Here, I used the same img src for each book, since each book’s respective image may not always be available. Change the image source, and you can also use a different image for each book.

Show a book’s info

The ShowBookDetails component has one task: it shows all the info we have about any book. We have both delete and edit buttons here to get access.

import React, { Component } from 'react'; import { Link } from 'react-router-dom'; import '../App.css'; import axios from 'axios'; class showBookDetails extends Component { constructor(props) { super(props); this.state = { book: {} }; } componentDidMount() { // console.log("Print id: " + this.props.match.params.id); axios .get('http://localhost:8082/api/books/'+this.props.match.params.id) .then(res => { // console.log("Print-showBookDetails-API-response: " + res.data); this.setState({ book: res.data }) }) .catch(err => { console.log("Error from ShowBookDetails"); }) }; onDeleteClick (id) { axios .delete('http://localhost:8082/api/books/'+id) .then(res => { this.props.history.push("/"); }) .catch(err => { console.log("Error form ShowBookDetails_deleteClick"); }) }; render() { const book = this.state.book; let BookItem = <div> <table className="table table-hover table-dark"> {/* <thead> <tr> <th scope="col">#</th> <th scope="col">First</th> <th scope="col">Last</th> <th scope="col">Handle</th> </tr> </thead> */} <tbody> <tr> <th scope="row">1</th> <td>Title</td> <td>{ book.title }</td> </tr> <tr> <th scope="row">2</th> <td>Author</td> <td>{ book.author }</td> </tr> <tr> <th scope="row">3</th> <td>ISBN</td> <td>{ book.isbn }</td> </tr> <tr> <th scope="row">4</th> <td>Publisher</td> <td>{ book.publisher }</td> </tr> <tr> <th scope="row">5</th> <td>Published Date</td> <td>{ book.published_date }</td> </tr> <tr> <th scope="row">6</th> <td>Description</td> <td>{ book.description }</td> </tr> </tbody> </table> </div> return ( <div className="ShowBookDetails"> <div className="container"> <div className="row"> <div className="col-md-10 m-auto"> <br /> <br /> <Link to="/" className="btn btn-outline-warning float-left"> Show Book List </Link> </div> <br /> <div className="col-md-8 m-auto"> <h1 className="display-4 text-center">Book's Record</h1> <p className="lead text-center"> View Book's Info </p> <hr /> <br /> </div> </div> <div> { BookItem } </div> <div className="row"> <div className="col-md-6"> <button type="button" className="btn btn-outline-danger btn-lg btn-block" onClick={this.onDeleteClick.bind(this,book._id)}>Delete Book</button><br /> </div> <div className="col-md-6"> <Link to={`/edit-book/${book._id}`} className="btn btn-outline-info btn-lg btn-block"> Edit Book </Link> <br /> </div> </div> {/* <br /> <button type="button" class="btn btn-outline-info btn-lg btn-block">Edit Book</button> <button type="button" class="btn btn-outline-danger btn-lg btn-block">Delete Book</button> */} </div> </div> ); } } export default showBookDetails;

Update a book’s info

UpdateBookInfo.js , as its name indicates, is responsible for updating a book’s info. An Edit Book button will trigger this component to perform. After clicking Edit Book, we will see a form with the old info, which we will be able to edit or replace.

import React, { Component } from 'react'; import { Link } from 'react-router-dom'; import axios from 'axios'; import '../App.css'; class UpdateBookInfo extends Component { constructor(props) { super(props); this.state = { title: '', isbn: '', author: '', description: '', published_date: '', publisher: '' }; } componentDidMount() { // console.log("Print id: " + this.props.match.params.id); axios .get('http://localhost:8082/api/books/'+this.props.match.params.id) .then(res => { // this.setState({...this.state, book: res.data}) this.setState({ title: res.data.title, isbn: res.data.isbn, author: res.data.author, description: res.data.description, published_date: res.data.published_date, publisher: res.data.publisher }) }) .catch(err => { console.log("Error from UpdateBookInfo"); }) }; onChange = e => { this.setState({ [e.target.name]: e.target.value }); }; onSubmit = e => { e.preventDefault(); const data = { title: this.state.title, isbn: this.state.isbn, author: this.state.author, description: this.state.description, published_date: this.state.published_date, publisher: this.state.publisher }; axios .put('http://localhost:8082/api/books/'+this.props.match.params.id, data) .then(res => { this.props.history.push('/show-book/'+this.props.match.params.id); }) .catch(err => { console.log("Error in UpdateBookInfo!"); }) }; render() { return ( <div className="UpdateBookInfo"> <div className="container"> <div className="row"> <div className="col-md-8 m-auto"> <br /> <Link to="/" className="btn btn-outline-warning float-left"> Show BooK List </Link> </div> <div className="col-md-8 m-auto"> <h1 className="display-4 text-center">Edit Book</h1> <p className="lead text-center"> Update Book's Info </p> </div> </div> <div className="col-md-8 m-auto"> <form noValidate onSubmit={this.onSubmit}> <div className='form-group'> <label htmlFor="title">Title</label> <input type='text' placeholder='Title of the Book' name='title' className='form-control' value={this.state.title} onChange={this.onChange} /> </div> <br /> <div className='form-group'> <label htmlFor="isbn">ISBN</label> <input type='text' placeholder='ISBN' name='isbn' className='form-control' value={this.state.isbn} onChange={this.onChange} /> </div> <div className='form-group'> <label htmlFor="author">Author</label> <input type='text' placeholder='Author' name='author' className='form-control' value={this.state.author} onChange={this.onChange} /> </div> <div className='form-group'> <label htmlFor="description">Description</label> <input type='text' placeholder='Describe this book' name='description' className='form-control' value={this.state.description} onChange={this.onChange} /> </div> <div className='form-group'> <label htmlFor="published_date">Published Date</label> <input type='date' placeholder='published_date' name='published_date' className='form-control' value={this.state.published_date} onChange={this.onChange} /> </div> <div className='form-group'> <label htmlFor="publisher">Publisher</label> <input type='text' placeholder='Publisher of this Book' name='publisher' className='form-control' value={this.state.publisher} onChange={this.onChange} /> </div> <button type="submit" className="btn btn-outline-info btn-lg btn-block">Update Book</button> </form> </div> </div> </div> ); } } export default UpdateBookInfo;

Back end (server side) again!

We just implemented all of our components! Now we need a little change in our server-side (back-end) project.

Changes required in our back end

If we try to call our back-end API from the front-end part, it gets an error that says: “Access to XMLHttpRequest at ‘http://localhost:8082/api/books’ from origin ‘http://localhost:3000’ has been blocked by CORS policy: Response to preflight request doesn’t pass access control check: No ‘Access-Control-Allow-Origin’ header is present on the requested resource.”

To solve this, we need to install cors in our back-end (server-side) project. Go to the project folder (e.g., MERN_A_to_Z ) and run:

$ npm install cors

Now, update app.js (the back end’s entry point) with the following code:

// app.js const express = require('express'); const connectDB = require('./config/db'); var cors = require('cors'); // routes const books = require('./routes/api/books'); const app = express(); // Connect Database connectDB(); // cors app.use(cors({ origin: true, credentials: true })); // Init Middleware app.use(express.json({ extended: false })); app.get('/', (req, res) => res.send('Hello world!')); // use Routes app.use('/api/books', books); const port = process.env.PORT || 8082; app.listen(port, () => console.log(`Server running on port ${port}`));

Run both front end and back end

Run the server

Now, run the server (inside the project folder):

$ npm run app

If you get any error, then follow the commands below (inside the project folder):

$ npm install $ npm run app

Run the client

From the front-end project directory, run the command below:

$ npm start

If you get an error, again, follow the same commands below:

$ npm install $ npm start

Conclusion

Let’s check everything in the browser. Open http://localhost:3000 in your browser. Now you can add a book, delete a book, show the list of books, and edit books. The following routes should perform accordingly:

Add a new book: http://localhost:3000/create-book

Show the list of books: http://localhost:3000/

Show any book’s info: http://localhost:3000/show-book/:id

Update a book’s info: http://localhost:3000/edit-book/:id

Congratulations! We have successfully finished our MERN stack A to Z tutorial. You can visit my GitHub to see both the server-side and client-side portions of the project.

Full visibility into production React apps Debugging React applications can be difficult, especially when users experience issues that are difficult to reproduce. If you’re interested in monitoring and tracking Redux state, automatically surfacing JavaScript errors, and tracking slow network requests and component load time, Debugging React applications can be difficult, especially when users experience issues that are difficult to reproduce. If you’re interested in monitoring and tracking Redux state, automatically surfacing JavaScript errors, and tracking slow network requests and component load time, try LogRocket LogRocket is like a DVR for web apps, recording literally everything that happens on your React app. Instead of guessing why problems happen, you can aggregate and report on what state your application was in when an issue occurred. LogRocket also monitors your app's performance, reporting with metrics like client CPU load, client memory usage, and more. The LogRocket Redux middleware package adds an extra layer of visibility into your user sessions. LogRocket logs all actions and state from your Redux stores. Modernize how you debug your React apps — start monitoring for free.