We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 33
201032019 React CRUD Example | MERN Stack Tutorial
> (
hotjar See how your visitors are really using your website. TRY IT FOR FREE
Home > Reacts >
REACTJS
React CRUD Example | MERN Stack Tutorial
@ vy rons astupdated vez, 2018
React CRUD Example | MERN Stack Tutorial is today’s leading topic. This article's goal is
to explain the by making the CRUD application. Most applications are CRUD applications.
If you don't know what CRUD is, it's short for Create, Read, Update and Delete. This
application is also called a MERN Stack application because we are using MongoDB,
Express, React, and Node,js tech stack. This article will show you the steps to create an
app where the user can create new business, fetch that business, edit business and
delete business using Reacts. Let u
Ifyou want to learn more about Rez
Complete Guide (incl. React Router
bid h ig fee) oi ed
FOR YOUR DORM
1 What is React?
2.Why use React?
hitpsifappdividend.com/2018/17/Ireaet-cud-example-mem-stacktutrall 1132010372019 React CRUD Example | MERN Stack Tutorial
hitpsifappdividend.com/2018/17/Ireaet-cud-example-mem-stack-tutrall
aaaas te
x)
3.
hotjar See how your visitors are really using your website. TRY IT FOR FREE
Pres Wunngure HEGLO VUUNE
8 #3: Create the bootstrap form
9#4; Submit the Form
10 #5: Create a backend on Node,js
11 #6: Setup a MongoDB database
12 #7: Create a Mongoose Schema
13 #8: Define the route for Node,js Express application
14 #9: Install Axios library and send a POST request.
15 #10: Display the backend data
16 #11: Edit and Update Functionality
17 #12: Delete the data
What is React?
React,js is UI Library and not a complete Framework like Angular. React is the JavaScript
library for building user interfaces. It is maintained by Facebook, Instagram and a
community of individual developers and corporations. You can find its official
documentation here.
Why use React?
React.js allows you to express how your app should look at any given point in time. React
will automatically manage all UI updates when your underlying data changes. When the
data changes, React conceptually hits the “refresh” button and knows to only update the
changed parts. The main reason to use React for your next project is following.
1. Fast Learning Curve.
2, Reusable Components.
3, Fast render with Virtual DOM.
4, Clean Abstraction.
5, Redux: A State Management L!"
6. Great Developer Tools.
7. React Native: You can build a
or iOS.
TON yh Aigsce) =o
Who uses React? meee MCW
Aside from Facebook and Instagra
Dropbox, PayPal, Netflix, Airbnb a
2199201032019 React CRUD Example | MERN Stack Tutorial
4. x
hotjar See how your visitors are really using your website. TRY IT FOR FREE
1, MacUs (Mojave)
2. Node v10.11.0
3. NPM v6.4.1
4, MongoDB shell version v3.6.3
5, MongoDB version v3.6.3
6, Reacts version 16.6.1
React CRUD Example
Create a new React app using the following command.
#1: Install React Application
npx create-react-app reactcrud
cd reactcrud
npm start.
fem es
Pence
SCR RU ec Ce a ee ead
pore ne eros rn eee tes
yarn add v1.9.4
ee a)
Pot c)
COME carey
Ce eee ee ee eC
Peers G ieee Ree CM tice eea ts MCR teeta
a ce
i
Deeg Nees
Corte seam tetsteme tear
ffer.from() methods instead
crac
Dee
ree satts
SRC nad
ee eee
To upgrade, run the follow
hitps:lappdividend.com/2018/111 react
srample-mem-stack-tutorill 133201032019 React CRUD Example | MERN Stack Tutorial
“3 — x
hotjar See how your visitors are really using your website. TRY IT FOR FREE
Go to this URL: http://localhost:3000/
Edit src/App. js and save to reload.
MEE Teabaccr-leis
Now, install the Bootstrap 4 Framework using the following command.
yarn add bootstrap
# or
npm install bootstrap --save
Import the Bootstrap CSS Framewd
Modify the code inside the src >> Aj
tps ifappdividend com/2018/17Ireaet-cud-example-mem-stack-tutrial! 4133201032019 React CRUD Example | MERN Stack Tutorial
>
hotjar See how your visitors are really using your website. TRY IT FOR FREE
class App extends Component {
render() {
return (
ch2>React CRUD Tutorial
wu
+
}
export default App;
Save the file and go to the browser and you can see that we have successfully integrated
bootstrap 4 in our react application.
#2: Configure React routing
Type the following command to install the react- router-dom module, If you want to find
more information about the react-router-dom module, then you can follow this
documentation.
yarn add react-router-dom
for
npn install react-router-dom --save
Go to the index,s file and Wrap the BrowserRouter object around the App,js component.
U/ index.js
import React from ‘react’;
import ReactDOM from ‘react-dom’ ;
import { BroserRouter } from ‘reac}
import App from ‘./App"
import * as serviceWorker from *./s|
ReactDOM. render(
bid h ig fee) oi ed
FOR YOUR DORM
, document .getélef
servicehorker.unregister();
Now, create three components.
hitpsifappdividendcom/2018/11/Ireaet-cud-exampl 8/33201032019 React CRUD Example | MERN Stack Tutorial
ee
>
hotjar See how your visitors are really using your website. TRY IT FOR FREE
2. edit.componentys
3. index.component js
// create. component. js
import React, { Component } from ‘react’;
export default class Create extends Component {
render() {
return (
)
x
// index. component. js
import React, { Component } fron ‘react’;
export default class Index extends Comnonen
render() {
return (
ceive
‘
hotjar See how your visitors are really using your website. TRY IT FOR FREE
import Create from
import Edit from
import Index from
-/components/create. component’;
/conponents/edit . component" ;
[components /index. component: ;
Class App extends Component {
render() {
return (
React CRUD Example Home Cresie index
Welcome to React CRUD Tutorial
Add New Business:
‘Add Person Name:
‘bd Busines Nae: bid h ig fee) oi ed
FOR'YOUR DORM
‘hdd OST Number:
hitpsifappdividend.com/2018/17/Ireaet-cud-example-mem-stack-tutrall 8133zonsz018 React CRUD Example |MERN Stack Ttoral
2, a
hotjar See how your visitors are really using your website. TRY IT FOR FREE
1. person name
2. business name
3. gst number
So we need to create four functions that can track the values of the textbox and set that
state according to it. Also, the fourth function will send the POST request to the node
express server.
Now, first, we will define the constructor and set the initial state and then also bind this to
the different events inside the constructor.
Then define the different functions with each input text values. So when the user types
inside the textbox, we set the state according to it.
So, let say, the user is typing the person name inside the textbox, we are changing the
state value of person name. Finally, same for all of the inputs and when we submit the
form, we get the values from the state and send to the POST request.
11 DIY PROJECTS:
FOR'YOUR DORM
hitpsifappdividend.com/2018/171 Ireaet-cud-example-mem-stack-tutrall 9133201032019 React CRUD Example | MERN Stack Tutorial
>
hotjar See how your visitors are really using your website. TRY IT FOR FREE
constructor(props) {
super(props) ;
‘this.onChangePersonName = this.onChangePersonNane.bind(this);
‘this.onChangeBusinessName = this.onChangeBusinessNane.bind(this) ;
‘this.onChangeGstNumber = this.onChangeGstNumber.bind( this);
‘this.onSubmit = this.onSubmit.bind(this);
this.state = {
person_name: '*,
business_name: *',
business_gst_number:
y
y
onchangePersonName(e) {
‘this. setState({
person_name: e.target.value
Ds
+
‘onChangeBusinessName(e) {
this. setstate({
business _nane: e.target.value
»
+
onchangeGstNumber(e) {
this. setstate({
business_gst_number: e.target.value
»)
y
onsubmit(e) (
e. preventdefault(
console.1og("The values are ${this.state.person_nane}, ${this.state.business_nane}, a
nd $(this.state.business_gst_nunber})
this. setstate({
person_nane: *',
business name: ‘*,
business_gst_nunber: '"
»
y
render() {
return (
Add New Business<
Person}
hotjar See how your visitors are really using your website. TRY IT FOR FREE
‘this. state.business_name}
onChange={ this ..onChangeBusinessName)
P
)
+
}
Now, submit the form with proper values and open your browser console panel and see
the response.
Al) samen cous sis mime >|
Be Tle |r omaeors |
React CRUD Example
Welcome to React CRUD Tutorial | Rent GLIA Re as
Add New Business
_— Ny) Ag s<0) =o 1 ed
crn FOR YOUR DORM
hitpsifappdividend.com/2018/17/Ireaet-cud-example-mem-stack-tutrall 19199201032019 React CRUD Example | MERN Stack Tutorial
Also, ngw check inside the mongodb database and see the values. x
hotiar see how your visitors are really using your website. TRY IT FOR FREE
To seeMhe values inside the database, we can start a mongoshell and logk.at the.valuss.las
the database.
oc ar)
oe ie 5 ee aoe
2018-11-11T21:28:12.618+@53@ I CONTROL [initandli:
Prmrarrer eesti bac Maree Dee eer
Rue CC
} 2018-11-11721:28:12.618+0530 I CONTROL [initandlisten] Sand
ee eet eter ene cate
Srey a Er Ue MmRGTeCIg eS Creed
ne Peru etn
Prirtaraerss Somer eec ures
iterfaces. If this behavior is desired, start the
Sresnhrsers es y Tome) Poa
Siete eee eee ey
2018-11-11721:28:12.618+0530 I CONTROL [initandlisten]
ecg)
ee RR asa)
> db. business. find().prettyO
{
aCe
fm
reer a Ge)
eee a ame CTL Sera
"v's @)
So, we can see that our data is added successfully.
#10: Display the backend data
Write the following code inside the index.componentjs file.
r
hitps:lappdividend.com/2018/111 react
srample-mem-stack-tutorill 20133,201032019 React CRUD Example | MERN Stack Tutorial
)
1 tp@ex. conponent,4s _ ; %
jotyar” Se How your visitors are really using your website. TRY IT FOR FREE
Amport React, { Component } from ‘react’; HIDE AD + AD VIA BUYSELLADS
import axios from ‘axios’;
import TableRow from *./TableRow’ ;
export default class Index extends Component {
constructor(props) {
super(props) ;
‘this.state = {business: []};
?
‘component DidMount (){
axios .get( ‘http: //localhost :48e@/business' )
-then(response => {
this.setState({ business: response.data });
»
scatch(function (error) {
console. log(error);
»
}
tabrow(){
return this.state.business.map(function(object, i){
return 3
ns
?
render() {
return (
Business List
Action
{ this.tabRow() }
3
?
y
So, here, we have sent the GET req 11 DIY PROJECTS
API. FOR'YOUR DORM
We have imported the TableRow,j
the components folder, create on}
following code inside it.
hitpsifappdividendcom/2018/17/ Ireaet-cud-exampl 213201032019 React CRUD Example | MERN Stack Tutorial
®
‘/ Tp@LeRow. js . . Ww
Ot Jar” See how your visitors are really using your website. TRY IT FOR FREE
Amport React, { Component } from ‘react’; HIDE AD + AD VIA BUYSELLADS
class TableRow extends Conponent {
render() {
return (