Sunday, July 16, 2017

REACT


React is a declarative, efficient, and flexible JavaScript library for building user interfaces.
It's used for handling view layer for web and mobile apps. ReactJS allows us to create reusable UI components. It is currently one of the most popular JavaScript libraries and it has strong foundation and large community behind it.


React Features

  • JSX − JSX is JavaScript syntax extension. It isn't necessary to use JSX in React development, but it is recommended.
  • Components − React is all about components. 
  • Unidirectional data flow and Flux − React implements one way data flow which makes it easy to reason about our app. 
  • License − React is licensed under the Facebook Inc. 


    React Advantages

  • React uses virtual DOM which is JavaScript object. This will improve apps performance since JavaScript virtual DOM is faster than the regular DOM.
  • React can be used on client and server side.
  • Component and Data patterns improve readability which helps to maintain larger apps.

    React can be used with other frameworks.

     React JSX

    React uses JSX for templating instead of regular JavaScript. It is not necessary to use it, but there are some pros that comes with it.

  • JSX is faster because it performs optimization while compiling code to JavaScript.
  • It is also type-safe and most of the errors can be caught during compilation.
  • JSX makes it easier and faster to write templates if you are familiar with HTML.
 
import React from 'react';

class App extends React.Component {
   render() {

      var i = 1;

      return (
         <div>
            <h1>{i == 1 ? 'True!' : 'False'}</h1>
         </div>
      );
   }
}

export default App;
 
 
 
  React component
 
This component is owner of Header and Content. We are creating Header and Content separately and just adding it inside JSX tree in our App component. Only App component needs to be exported.

App.jsx

import React from 'react';

class App extends React.Component {
   render() {
      return (
         <div>
            <Header/>
            <Content/>
         </div>
      );
   }
}

class Header extends React.Component {
   render() {
      return (
         <div>
            <h1>Header</h1>
         </div>
      );
   }
}

class Content extends React.Component {
   render() {
      return (
         <div>
            <h2>Content</h2>
            <p>The content text!!!</p>
         </div>
      );
   }
}
 
React State 

State is the place where the data comes from.If we have, for example, ten components that need data 
from the state, we should create one container component that will keep
 the state for all of them.


App.jsx

import React from 'react';

class App extends React.Component {
   constructor(props) {
      super(props);
		
      this.state = {
         header: "Header from state...",
         content: "Content from state..."
      }
   }
	
   render() {
      return (
         <div>
            <h1>{this.state.header}</h1>
            <h2>{this.state.content}</h2>
         </div>
      );
   }
}

export default App;

3 comments: