React
React
React
1. INTRODUCTION
React allows developers to create large web-applications that use data that can change over
time, without reloading the page. It aims primarily to provide speed, simplicity and
scalability. React processes only user interfaces in applications. This corresponds to View in
the Model-View-Controller (MVC) template, and can be used in combination with
other JavaScript libraries or frameworks in MVC, such as AngularJS.
1.1.1Features
While the Reacts brothers (Angular, Ember, Backbone) are dubbed as client-
side MVCframeworks, this guy is different. His creators call him the V of MVC. In other
words. he entirely focuses on View which describes the User Interaction part.
React is the fastest of the bunch. He carefully notes down the differences in his in-memory
cache and use these differences to update the DOM in browser, which is what gives him the
boost. This is the Virtual DOM feature.
Other notable features include One way dataflow and JSX (the react components are
basically JSX).
1.1.2 History
2. WHY REACT?
V(view) of MVC - Solution of View in MVC
Virtual DOM - Reactjs use the concept of virtual DOM which helps in the
performance
Unidirectional Data Flow - Compare to the 2 way data binding. Reactjs use the
concept of Unidirectional data flow which improve the over all performance.
SEO Friendly - Components are client side as well as server side render hence they
3. REACT FEATURES
JSX JSX is JavaScript syntax extension. It isn't necessary to use JSX in React
development, but it is recommended.
Unidirectional data flow and Flux React implements one way data flow which
makes it easy to reason about your app. Flux is a pattern that helps keeping your data
unidirectional.
License React is licensed under the Facebook Inc. Documentation is licensed under
CC BY 4.0.
React uses virtual DOM which is JavaScript object. This will improve apps
performance since JavaScript virtual DOM is faster than the regular DOM.
Component and Data patterns improve readability which helps to maintain larger
apps.
React is using inline templating and JSX. This can seem awkward to some
developers.
Getting React up and running is not as simple as downloading one large piece of software.
You will need to install many, smaller software packages.
The first thing to install is a good installer! You need a way to download and install software
packages easily, without having to worry about dependencies.
In other words, you need a good package manager. We'll be using a popular package manager
named npm. npm is a great way to download, install, and keep track of JavaScript software.
You can install npm by installing Node.js. Node.js is an environment for developing server-
side applications. When you install Node.js, npm will install automatically.
You should see links to download Node.js. Click on the download link of your choice.
Follow the subsequent instructions to install Node.js and npm. If you've already
installed Node.js, that's okay, do it anyway.
When you install software, you may be used to something like this: you install what you
need, it sits there on your computer, and you can use it whenever you want.
You can do that with npm! But there's a different, better way: install what you need, over and
over again, every time that you need it. Download and install React every time that you make
a React app.
First, npm makes installation extremely easy. Installing software over and over sounds like a
headache, but it really isn't. We'll walk through how soon!
Second, npm modules ("modules" are another name for software that you download via npm)
are usually small. There are countless modules for different specific purposes. Instead of
starting your app with a giant framework that includes tons of code you don't need, you can
install only modules that you will actually use! This helps keep your code quick, easy to
navigate, and not vulnerable to dependencies that you don't understand.
IN CONCLUSION: Starting now, every step in this article series will be a step that you have
to take every time you make a new React app. You don't have to
install Node.js and npm anymore, but you should start from here for every new React project
that you make.
C:\Users\username\Desktop>mkdir reactApp
C:\Users\username\Desktop\reactApp>npm init
Since we want to use React, we need to install it first. The --save command will add these
packages to package.json file.
We already mentioned that we will need some babel plugins so let's install it too.
C:\Users\username\Desktop\reactApp>touch index.html
C:\Users\username\Desktop\reactApp>touch App.jsx
C:\Users\username\Desktop\reactApp>touch main.js
C:\Users\username\Desktop\reactApp>touch webpack.config.js
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;
Open the package.json and delete "test" "echo \"Error: no test specified\" && exit
1" inside "scripts" object. We are deleting this line since we will not do any testing in this
tutorials. Let's add the start command instead.
Now we can use npm start command to start the server. --hot command will add live reload
after something is changed inside our files so we don't need to refresh the browser every
time we change our code.
Step 6 - index.html
This is just regular HTML. We are setting div id = "app" as a root element for our app and
adding index.js script which is our bundled app file.
<!DOCTYPE html>
<head>
<title>React App</title>
</head>
<body>
<div id = "app"></div>
</body>
</html>
App.jsx
render() {
return (
<div>
Hello World!!!
</div>
);
We need to import this component and render it to our root App element so we can see it in
browser.
main.js
C:\Users\username\Desktop\reactApp>npm start
It will show you the port we need to open in browser, in our case http://localhost:8080/.
After we open it we will see the following output:
5. CORE CONCEPTS
1. JSX
2. VIRTUAL DOM
3. UNIDIRECTIONAL DATAFLOW
4. COMPONENTS
5.1 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.
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.
Using JSX
JSX looks like regular HTML in most cases. We already used it in environment setup
tutorial. Look at the code from App.jsx where we are returning div.
App.jsx
render() {
return (
<div>
Hello World!!!
</div>
);
Even though it's similar to HTML, there are a couple of things you need to keep in mind
when working with JSX.
Attributes
You can use your own custom attributes in addition to regular HTML properties and
attributes. When you want to add custom attribute, you need to use data- prefix. In example
below we added data-myattribute as an attribute of p element.
render() {
return (
<div>
<h1>Header</h1>
<h2>Content</h2>
</div>
);
JavaScript Expressions
JavaScript expressions can be used inside of JSX. You just need to wrap it with curly
brackets {}. Example below will render 2.
render() {
return (
<div>
<h1>{1+1}</h1>
</div>
);
You can not use if else statements inside JSX but you can use conditional
(ternary) expressions instead. In example below variable i equals to 1 so the browser will
render true, if we change it to some other value it will render false.
render() {
var i = 1;
return (
<div>
</div>
);
Styling
React recommends using inline styles. When you want to set inline styles, you need to
use camelCase syntax. React will also automatically append px after the number value on
specific elements. You can see below how to add myStyle inline to h1 element.
render() {
var myStyle = {
fontSize: 100,
color: '#FF0000'
return (
<div>
</div>
);
Comments
When writing comments you need to put curly brackets {} when you want to write comment
within children section of a tag. It is good practice to always use {} when writing comments
since you want to be consistent when writing the app.
render() {
return (
<div>
<h1>Header</h1>
</div>
);
Naming Convention
HTML tags are always using lowercase tag names, while React components starts
with Uppercase.
In React, for every DOM object, there is a corresponding "virtual DOM object." A
virtual DOM object is a representation of a DOM object, like a lightweight copy.
A virtual DOM object has the same properties as a real DOM object, but it lacks the real
thing's power to directly change what's on the screen.
Manipulating the DOM is slow. Manipulating the virtual DOM is much faster, because
nothing gets drawn onscreen. Think of manipulating the virtual DOM as editing a blueprint,
as opposed to moving rooms in an actual house.
How it helps
When you render a JSX element, every single virtual DOM object gets updated.
This sounds incredibly inefficient, but the cost is insignificant because the virtual DOM can
update so quickly.
Once the virtual DOM has updated, then React compares the virtual DOM with a virtual
DOM snapshot that was taken right before the update.
By comparing the new virtual DOM with a pre-update version, React figures out exactly
which virtual DOM objects have changed. This process is called "diffing."
Once React knows which virtual DOM objects have changed, then React updates those
objects, and only those objects, on the real DOM. In our example from earlier, React would
be smart enough to rebuild your one checked-off list-item, and leave the rest of your list
alone.
This makes a big difference! React can update only the necessary parts of the DOM. React's
reputation for performance comes largely from this innovation.
In summary, here's what happens when you try to update the DOM in React:
The virtual DOM gets compared to what it looked like before you updated it. React figures
out which objects have changed.
The changed objects, and the changed objects only, get updated on the realDOM.
Traditional DOM
- In Reactjs application the data flow from the parent to the children component by the state
and the props.
- Only one parent is responsible to update the states and passing the value to the children
components via props.
- setState is used to update/refresh the UI when the state change and the value can be pass to
the children component by the this.props
To fully understand the benefits of unidirectional data flow, you must first understand the
inherent issues with the alternative. The alternative, of course, is bidirectional data flow. This
data paradigm typically manifests itself as some sort of model binding. For many years,
application architecture has revolved around the model-view-controller, or MVC, pattern. In
this pattern, a model is responsible to storing and retrieving application data. The view is the
presentational piece of the applicationwhat the end-user sees. The controller is responsible
for transforming data from a model so that it can be presented via the view.
For a long time, this pattern was exactly what we as developers needed. It offered separation
of concerns. The view shouldnt be concerned with where the data came from, and vice versa.
The model shouldnt concern itself with how the data is presented. This pattern works great
for simple applications, but quickly starts to break down as applications become more
complex. Many applications rely on user interaction to drive data and application state, which
puts greater responsibility on the controller. It has to both maintain application state, and act
as a mediator between the view and model. This complexity led us to model-binding, which
allowed developers to auto-bind user interface changes to changes in the data model, and vice
versa. Herein lies the problem: Application data and state are being manipulated from two
sources and more or less bypassing the controller. The current state of the application is no
longer predictable.
Unidirectional data flow, on the other hand, provides predictable application state. In a
typical unidirectional application architecture, changes in an application view layer trigger an
action within the data layer. Those changes are then propagated back to the view. It is
important to note here that the view does not directly affect the application data.
5.4 COMPONENTS
Everything in reactjs is components. The core building blocks of React application is
components only. Components interact with each other and maintain the state too. In Reactjs
whole application is need to be break into the component only.
componentDidMount is executed after first render only on the client side. This is
where AJAX requests and DOM or state updates should occur. This method is also
used for integration with other JavaScript frameworks and any functions with
delayed execution like setTimeoutor setInterval. We are using it to update the state
so we can trigger the other lifecycle methods.
6.CONCLUSION
ReactJS is a library that generates the view layer of an application based on its state.
ReactJS applications are built from React Components - independent resusable components
that describe how the UI should look based on their own state and properties. Somany
websites are using react, React one of the most demanded career now.
7. REFERENCES
1. https://www.google.co.in
2. https://facebook.github.io/react
3. https://www.tutorialspoint.com/reactjs
4. https://www.edx.org/
5. http://opensourceforu.com/?s=reactJS
6. https://scotch.io/