Build A ReactJS Powered CRUD Application

React was first deployed on Facebook's Newsfeed in 2011. After a full year of testing, it was next deployed on Instagram in 2012.

ReactJS is a popular option for creating Javascript powered UI. It is commonly used for building the UI of single-page apps. In the app's architecture, it is used to develop and handle the View layer. The best thing about React is that you could create reusable components that could be used in similar projects.

React is mainly used to create web applications in which data is changed without reloading the page. In effect, React is simple to use, scalable and fast enough for all major web apps. The library ensures that the View component of the MVC model and works vry well with all other libraries at this layer such as Angular.

In this article , I will demonstrate the simple CRUD application using React.

Prerequisites

For the purpose of this tutorial, I assume that you have a PHP application installed on a web server. My setup is,

  • PHP 7.1
  • Node.js with NPM
  • React

To make sure that that I don’t get distracted by server level issues, I decided to host my PHP Cloud on Cloudways managed servers because it takes care of server level issues and has a great devstack right out of the box. You can try out Cloudways for free by signing up for an account .

Install Node.js with NPM

The first step is the installation of Node.js with NPM.

For this, first install Node.js. Next go to the project’s folder and type the following command in the terminal:

  1. npm init
  2. npm install

Create the React Application

After successfully installing npm, I will go to the SSH terminal, and type the following command to create the React app folder.

  1. create-react-app my-app
  2. cd my-app
  3. npm start

Constructor

Let’s open App.js and put the following code in the App class,

  1. constructor(props) {  
  2.     super(props);  
  3.     this.state = {  
  4.         title: 'Simple React curd Apps',  
  5.         act: 0,  
  6.         index: '',  
  7.         datas: []  
  8.     }  
  9. }  

 

Create the View

First, I will create the view. In this case, I will create a basic form structure that contains the Name and Information fields. Next, open the App.js file, and paste the following code in return()

  1. <div className="App">  
  2.     <header className="App-header"> <img src={logo} className="App-logo" alt="logo" />  
  3.         <h1 className="App-title">{this.state.title}</h1>  
  4.     </header>  
  5.     <form ref="myform" className="myForm"> <input type="text" ref="name" placeholder="Your Name" className="formfield" /> <input type="text" ref="address" placeholder="Your Information" className="formfield" /> <button onClick={this.fSubmit} className="myButton"> Submit</button> </form>  
  6.     <pre>  
  7. {datas.map((data , i) =>  
  8. <li key={i} className="myList">  
  9. {i + 1} . {data.name} , {data.address}  
  10. <button onClick={() =>this.fEdit(i)} className="myListButton">Edit</button>  
  11. <button onClick={() =>this.fRemove(i)} className="myListButton">remove</button>  
  12. </li> )}  
  13. </pre> </div>  

 

 In the above code, I created a basic form and then called the title. I also mapped the concepts around the usage of the “map” method to traverse and display a list of similar objects representing a component in ReactJS.

 

CSS File

For styling the application , I wrote my own css that you could add to app.css file. If you have your own ideas about styling, you could easily add your own css.

  1. .App {  
  2.     text - align: center;  
  3.     margin: auto;  
  4.     width: 50 % ;  
  5. }.myForm {  
  6.     width: 95 % ;  
  7.     background - color: #dfee;  
  8.     border - radius: 5 px;  
  9.     padding: 20 px;  
  10. }.App - logo {  
  11.     animation: App - logo - spin infinite 20 s linear;  
  12.     height: 80 px;  
  13. }.App - header {  
  14.     background - color: #222;  
  15. height: 150px;  
  16. padding: 20px;  
  17. color: white;  
  18. }  
  19. .App-title {  
  20. font-size: 1.5em;  
  21. }  
  22. .App-intro {  
  23. font-size: large;  
  24. }  
  25. .formfield{  
  26. width: 100%;  
  27. padding: 12px 20px;  
  28. margin: 8px 0;  
  29. display: block;  
  30. border: 1px solid # ccc;  
  31.     border - radius: 5 px;  
  32.     box - sizing: border - box;  
  33. }.myButton {  
  34.     width: 100 % ;  
  35.     background: #14d182;  
  36. color:white;  
  37. padding: 14px 20;  
  38. margin: 8px 0px;  
  39. border: none;  
  40. border-radius: 4px;  
  41. cursor: pointer;  
  42. }  
  43. .myList{  
  44. list-style: none;  
  45. }  
  46. .myListButton{  
  47. display: inline-block;  
  48. height: auto;  
  49. width:auto;  
  50. border-radius: 5px;  
  51. border:0;  
  52. padding:1%;  
  53. margin:1%;  
  54. background-color:# 14 d182;  
  55.     color: white  
  56. }  
  57. @keyframes App - logo - spin {  
  58.     from {  
  59.         transform: rotate(0 deg);  
  60.     }  
  61.     to {  
  62.         transform: rotate(360 deg);  
  63.     }  
  64. }  

 

Add Component

To add components, I first use ComponentDidMount because it is executed after the first render, only on the client side. This is also the area where AJAX requests and/or state updates (DOM) happens. This method is also used for adding integration with other JavaScript frameworks and any functions that offer delayed functionality execution (for instance for setTimeoutor setInterval).

In this case, I need focus() on name. Thus, I have used it inside ComponentDidMount this.refs.name.focus().

  1. componentDidMount() {  
  2.     this.refs.name.focus();  
  3. }  
  4. fSubmit = (e) => {  
  5.     e.preventDefault();  
  6.     console.log('try');  
  7.     let datas = this.state.datas;  
  8.     let name = this.refs.name.value;  
  9.     let address = this.refs.address.value;  
  10.     if (this.state.act === 0) { //new  
  11.         let data = {  
  12.             name,  
  13.             address  
  14.         }  
  15.         datas.push(data);  
  16.     } else {  
  17.         let index = this.state.index;  
  18.         datas[index].name = name;  
  19.         datas[index].address = address;  
  20.     }  
  21.     //datas.push(data);  
  22.     this.setState({  
  23.         datas: datas,  
  24.         act: 0  
  25.     });  
  26.     this.refs.myform.reset();  
  27.     this.refs.name.focus();  
  28. }  

 

This is how the application will look at this point:

 

Edit Component

For the Edit component, just copy the following code and paste it into the app.js file,

  1. fEdit = (i) => {  
  2.     let data = this.state.datas[i];  
  3.     this.refs.name.value = data.name;  
  4.     this.refs.address.value = data.address;  
  5.     this.setState({  
  6.         act: 1,  
  7.         index: i  
  8.     });  
  9.     this.refs.name.focus();  
  10. }  

 


Just edit and click the Submit button to update your records.
 
Remove Component

For the Remove component, paste the following code in the app.js file .

  1. fRemove = (i) => {  
  2.     let datas = this.state.datas;  
  3.     datas.splice(i, 1);  
  4.     this.setState({  
  5.         datas: datas  
  6.     });  
  7.     this.refs.myform.reset();  
  8.     this.refs.name.focus();  
  9. }  

 

I have remove the first record and you can see the result.

 

Conclusion

React is very a simple and lightweight library that only deals with the view layer. It is not a beast like other MVC frameworks such as Angular or Ember. Any Javascript developer can understand the basics and start developing an awesome web application after only a couple of days reading tutorials.

Next Recommended Reading Bind Data To Table Using ReactJS And MVC