React JS curd tutorial for Beginners

react js

1.About the Tutorial:

React is a front-end library developed by Facebook. It is utilized for handling the view layer for web and mobile apps. ReactJS sanctions us to engender reusable UI components. It is currently one of the most popular JavaScript libraries and has a vigorous substructure and sizably voluminous community behind it.

2.Prerequisites:

If you optate to work with ReactJS, you require to have solid erudition of JavaScript, HTML5, and CSS. Even though ReactJS doesn’t utilize HTML, the JSX is kindred so your HTMLcognizance will be very auxiliary. We will explicate this more in one of the next chapters. We will additionally use EcmaScript 2015 syntax so any erudition in this area can be subsidiary.

Step 1 -Install Global Packages

We will require to install several packages for this setup. We will require some of the babel plugins, so let’s first install babel by running the following code in the command prompt window.

C:\Users\manoj>npm install -g babel
C:\Users\manoj>npm install -g babel-cli

Step 2 -Create the Root Folder

The root folder will be denominated reactApp and we will place it on Desktop. After the folder is engendered, we require to open it and engender empty package.json file inside by running npminit from the command prompt and follow the injuctive authorizations.

Step 3 -Add Dependencies and Plugins

We will use webpack bundler in these tutorial. Let’s install webpack and webpack-devserver.

C:\Users\manoj>npm install webpack --save
C:\Users\manoj>npm install webpack-dev-server --save

Since we optate to utilize React, we require to install it first. The –preserve command will integrate these packages to package.json file.

C:\Users\manoj\Desktop\reactApp>npm install react --save
C:\Users\manoj\Desktop\reactApp>npm install react-dom --save

As already mentioned, we will need some babel plugins, so let’s install it too

C:\Users\manoj\Desktop\reactApp>npm install babel-core
C:\Users\manoj\Desktop\reactApp>npm install babel-loader
C:\Users\manoj\Desktop\reactApp>npm install babel-preset-react
C:\Users\manoj\Desktop\reactApp>npm install babel-preset-es2015

Step 4 -Create the Files

Let’s engender several files that we require. It can be integrated manually or utilizing the command prompt.

C:\Users\manoj\Desktop\reactApp>touch index.html
C:\Users\manoj\Desktop\reactApp>touch App.jsx
C:\Users\manoj\Desktop\reactApp>touch main.js
C:\Users\manoj\Desktop\reactApp>touch webpack.config.js

Step 5 -Set Compiler, Server and Loaders

Open webpack-config.js file and integrate the following code. We are setting webpack ingress point to be main.js. Output path is the place where bundled app will be accommodated. We are additionally setting the development server to 8080 port. You can cull any port you optate.
And lastly, we are setting babel loaders to probe for js files, and use es2015 and react presets that we installed afore.

webpack.config.js

var config = {
 entry: './main.js',
 output: {
 path:'./',
 filename: 'index.js',
 },
 devServer: {
 inline: true,
 port: 8080
 },
 module: {
ReactJS
10
 loaders: [
 {
 test: /\.jsx?$/,
 exclude: /node_modules/,
 loader: 'babel',
 query: {
 presets: ['es2015', 'react']
 }
 }
 ]
 }
}
module.exports = config;

Open the package.json and expunge “test” “echo \”Error: no test designated\” && exit 1″ inside “scripts” object. We are expunging this line since we will not do any testing in this tutorial. Let’s integrate the commencement command instead

"start": "webpack-dev-server --hot"

Now, we can utilize npm start command to commence the server. –sultry command will integrate live reload after something is transmuted inside our files so we don’t require to refresh the browser every time we transmute our code.

Step 6 -index.htm

This is just conventional HTML. We are setting div id = “app” as a root element for our app and integrating index.js script, which is our bundled app file

<!DOCTYPE html>
<html lang = "en">
 <head>
 <meta charset = "UTF-8">
 <title>React App</title>
 </head>
 <body>
 <div id = "app"></div>
 <script src = "index.js"></script>
 </body>
</html>

Step 7 -App.jsx and main.js

This is the first React component. We will expound React components in depth in a subsequent chapter. This component will render Hello World!!!

App.jsx

import React from 'react';
class App extends React.Component {
 render() {
 return (
 <div>
 Hello World!!!
 </div>
 );
 }
}
export default App;

We require to import this component and render it to our root App element, so we can optically discern it in the browser.

main.js

import React from 'react';
import ReactDOM from 'react-dom';
import App from './App.jsx';
ReactDOM.render(<App />, document.getElementById('app'));

Step 8 -Running the Server

The setup is complete and we can start the server by running the following command.

C:\Users\manoj\Desktop\reactApp>npm start

It will show the port we require to open in the browser. After we open it, we will optically discern the following output.

About OakML

Hi we are enthusiastic developers team, we try to serve some good idea to open source community

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!