Azio's World

Powered by Blogger.

ReactJS is currently one of the most popular JavaScript libraries developed by Facebook.Its a front-end development library and used for handle view layer of mobile and web apps.


Pros:

  React virtual DOM will improve apps performance.
  You can render React on the server-side.
  ReactJS can use with any framework.Such as Backbone js, Angular js.

Cons:

    Reactjs is only a view layer. Integrating Reactjs into a traditional MVC framework such as rails would require    some configuration.
    There is a learning curve for beginners who are new to web development.

 Why should use ReactJS:

    Reactjs works great for teams, strongly enforcing UI and workflow  patterns.
    The user interface code is readable and maintainable.
    There is now a lot of demand for developers with ReactJS experience.

Prerequisites:

   JavaScript
   HTML 5
   CSS
   and basic knowledge of Ecma Script 2015

We will need NodeJS and NPM.





First we need to setup some babels plugins.You can install babel by running the following code in command prompt.

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

Then install webpack bundler and 'webpack-div-server'.Enter following commands for install those bundles.
C:\Users\username>npm install webpack --save
C:\Users\username>npm install webpack-dev-server --save


Then create a root folder and initialize with npm.
C:\Users\username\Desktop>mkdir myApp
C:\Users\username\Desktop>cd myApp


C:\Users\username\Desktop\myApp>npm init

Then it's time to install react and dom packages, '--save' command will add these packages to package.json file.
C:\Users\username\Desktop\reactApp>npm install react --save
C:\Users\username\Desktop\reactApp>npm install react-dom --save


Then install babel plugins to the created project.

C:\Users\username\Desktop\myApp>npm install babel-core
C:\Users\username\Desktop\myApp>npm install babel-loader
C:\Users\username\Desktop\myApp>npm install babel-preset-react
C:\Users\username\Desktop\myApp>npm install babel-preset-es2015

Then you need to create the following files inside 'myApp' folder.

index.html
App.jsx
main.js
webpack.config.js

Then add following codes to those files.


webpack-config.js

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

module.exports = config;

First we add main.js as entry point.We are setting development server to 8080 port. You can choose any other port you prefer.Also we are adding  babel loaders to search for js files and use es2015 and react presets.


package.json



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

index.html


<!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>
div id = "app" is root element for our app.

App.jsx



import React from 'react';

class App extends React.Component {
   render() {
      return (
         <div>
            Hello World!!!
         </div>
      );
   }
}

export default App;
This is the first react component.This component will render Hello World!!!.


main.js
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App.jsx';

ReactDOM.render(<App />, document.getElementById('app'));
We need to import this component and then render it to our root App element so we can see it in browser.

Then it's time to run our first app so enter following command to run the server.
C:\Users\username\Desktop\myApp>npm start






Share
Tweet
Pin
Share
No Comments
Newer Posts
Older Posts

Blog Archive

  • February 2023 (1)
  • May 2021 (2)
  • April 2017 (1)
  • March 2017 (1)
  • February 2017 (1)

Popular Posts

  • How to add JCalendar component to NetBeans IDE palette
    How to add JCalendar component to NetBeans IDE palette Who wants to implement a datepicker module in your application developed with Net...
  • Kruskal’s Minimum Spanning Tree Algorithm
    Spanning tree is a subgraph ,which is also a tree,that has all the vertices that is included in the original graph and they vertices are ...
  • Environment setup for ReactJS
    ReactJS is currently one of the most popular JavaScript libraries developed by Facebook.Its a front-end development library and used for...
  • Triple extortion of ransomware
    Ransomware is a type of malware that encrypts a victim's computer files.The attacker demands money from the victim to restore his comput...
  • Basic Color Theory for web UI/UX
    Across human history, master painters and other artists have earned global recognition for their ability to manipulate colors. In the moder...
  • Anti-patterns in Software Engineering
     Software development is a complex and dynamic field, and it requires the application of various best practices and techniques to ensure tha...

Created with by ThemeXpose