React is a javascript library created by Facebook. With help of this library, we can easily pass data to render in a view. React Js is very easy to use and you simply add React Js in any project. In this post, I simply explain how we can use React in our web application. I try to cover most of React features in my upcoming posts.

Official Website: ReactJS

Setup ReactJs in your Project

Setup of React Js is very easy you can download React library file or you can use their CDN. So I’m going to us their CDN. So first create new file name it whatever you like (.html) and write basic structure of Html in it. Now inside <head> tag you need to add ReactJs library and also babel library in it

What is babel?

Babel is javascript compiler. It allows you to write HTML code inside Javascript you can check this example to clearly understand.

Now once you complete this you are ready to create your first application. Now create a <div id=”test”> inside a body tag and also assign ID to it, this div is the main container where or React render its view. Now we can write or React Js code, to write you need to start <script type=”text/babel“></script> tag to write your code.

ReactDOM.render() allow us to render a view inside given target.

You can check output in this video

Components in React JS

Components is basically a view which you render. It allows you to reuse your view multiple places with different data. In simple word, every thing in a website is a component eg. Button, Menu, Gallery etc. So in react it is very easy to create a component and reuse them.

My first component in React Js

To create your first component you simply first create new file and add same code we create above after this, we can strat creating our component to create your component you need to call React class as shown bellow

Now in above code first we create a variable (MyComp) this variable is my component and in this, I call React CreateClass which help to create a view for my component. Now inside return yo need to keep in mind that only One parent is allowed you can not return multiple parent/element. eg.

this will give you an error so remember to return only one child inside return. After this, you can call you component (MyComp) inside render like HTML tag (<MyComp />) and also one important thing variable is in the capital form otherwise it gives an error. You can check output of above sample

Complete Code of above sample.

Pass data to component

Now we need to pass data to the component it is very easy to pass data to the component. We can pass data using attribute and set data in view using {} curly braces and with the help of (this.props.attribute_name) we can set data in view as shown in bellow

You also render your view multiple time with different data as I do in above code bu note we do this inside <div> tag as an already mention you need to return only one parent in it inside <div> you can run your component as many time as you want.

You can check the output of above sample in this video

This is the first post on ReactJs tutorial. Stay tuned to next tutorial on ReactJs. This is very basic and I try to make all example as simple and easy so you can understand basics of ReactJs. hope this post helps you Please like share and subscribe to my facebook and youtube channel 🙂