Learn React JS

Introduction

 
React js framework was developed by Facebook.
 
To include react in our HTML application.
  1. <scriptsrc="https://fb.me/react-0.12.2.js"></script>  
  2. <scriptsrc="https://fb.me/JSXTransformer-0.12.2.js"></script>  
To write the code we need to create .jsx file instead of .js files.
 
Create one main.jsx file and include that file in your index.html page.
  1. <scripttype="text/jsx"src="main.jsx">  
  2. </script>  
Open main.jsx file and add below code:
  1. React.render(  
  2.    <h1>hello World 1234</h1>,  
  3.    document.body  
  4. );  
React.render method is having two parameters. First one is HTML content or we can create user defined tag which is known as component in react terms and the second value is rendering with html tag.
 
React.CreateClass using this way we can create new components in react. Find below syntax to create new component.
  1. var Hello = React.createClass({  
  2.    render: function() {  
  3.       return<p>Hello, world!</p>;  
  4.    }  
  5. });  
After adding this code we need to do one small modification in React.render method.
  1. React.render(  
  2.    <Hello>,  
  3.    document.body  
  4. );  
Or
  1. React.render(  
  2.    newHello(),  
  3.    document.body  
  4. );  
What are Props and States?
 
Props: When we need to pass the data to class then it is known as props or we need to pass the data in HTML page.
 
States: When we need to read the data from HTML page.
 
Please find below example.
 
Props
  1. var Hello = React.createClass({  
  2.    render: function() {  
  3.       return<p>{this.props.message}</p>;  
  4.    }  
  5. });  
  6.   
  7. React.render(  
  8.    newHelloWorld({ message: "Hi, Hello World" }),  
  9.    document.body  
  10. );  
We are passing data through property message and binding that with html tags. This is the actual use of props.
  1. var Counter = React.createClass({  
  2.      getInitialState: function() {  
  3.           return {  
  4.                count: 0  
  5.           };  
  6.      },  
  7.      increment: function() {  
  8.           this.setState({  
  9.                count: this.state.counter + 1  
  10.           });  
  11.      },  
  12.      render: function() {  
  13.           return <div>  
  14.      <div>  
  15.           {  
  16.           this.state.count  
  17.           } </div> <buttononClick={ this.increment }> Click me </button> </div>;  
  18.      }  
  19. });  
  20. React.render(new Counter(), document.body); 
Count is the property that is bound with html page but that can be read from the application and reused and again displayed on the application this can be achieved using state only.
 
Creating a nested class: Here we have created two classes. One is Hello and second is nestedHello.
 
The class nestedHello is returning Hello class. And finally rendering Hello() to html body.
  1. var Hello = React.createClass(  
  2. {  
  3.     render: function()  
  4.     {  
  5.         return <p > hello nested class application < /p>;  
  6.     }  
  7. });  
  8. varnestedHello = React.createClass(  
  9. {  
  10.     render: function()  
  11.     {  
  12.         return <Hello /> ;  
  13.     }  
  14. });  
  15. React.render(  
  16.     new Hello(),  
  17.     document.body  
  18. );