Learn React JS

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 component 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. {  
  3.     getInitialState: function()  
  4.     {  
  5.         return {  
  6.             count: 0  
  7.         };  
  8.     },  
  9.     increment: function()  
  10.     {  
  11.         this.setState(  
  12.         {  
  13.             count: this.state.counter + 1  
  14.         });  
  15.     },  
  16.     render: function()  
  17.     {  
  18.         return <div >  
  19.             <  
  20.             div >  
  21.             {  
  22.                 this.state.count  
  23.             } < /div> <  
  24.             buttononClick = {  
  25.                 this.increment  
  26.             } > Click me < /button> <  
  27.             /div>;  
  28.     }  
  29. });  
  30. React.render(  
  31.     new Counter(),  
  32.     document.body  
  33. );  
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 nested class: Here we have created two class. 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. );  

 

Ebook Download
View all
Learn
View all