Reactjs Tutorial
Reactjs Tutorial
1
ReactJS
Audience
This tutorial will help JavaScript developers who look ahead to deal with ReactJS for the
first time. We will try to introduce every concept by showing simple code examples that
can be easily understood. After finishing all the chapters, you will feel confident working
with ReactJS. As a bonus we will introduce additional elements that work well with ReactJS
to help you learn the best practices and follow the modern JavaScript trends.
Prerequisites
If you want to work with ReactJS, you need to have solid knowledge of JavaScript,
HTML5, and CSS. Even though ReactJS doesn't use HTML, the JSX is similar so your HTML
knowledge will be very helpful. We will explain this more in one of the next chapters. We
will also use EcmaScript 2015 syntax so any knowledge in this area can be helpful.
All the content and graphics published in this e-book are the property of Tutorials Point (I)
Pvt. Ltd. The user of this e-book is prohibited to reuse, retain, copy, distribute or republish
any contents or a part of contents of this e-book in any manner without written consent
of the publisher.
We strive to update the contents of our website and tutorials as timely and as precisely as
possible, however, the contents may contain inaccuracies or errors. Tutorials Point (I) Pvt.
Ltd. provides no guarantee regarding the accuracy, timeliness or completeness of our
website or its contents including this tutorial. If you discover any errors on our website or
in this tutorial, please notify us at [email protected]
2
ReactJS
Table of Contents
About the Tutorial ........................................................................................................................................... 2
Audience.......................................................................................................................................................... 2
Prerequisites.................................................................................................................................................... 2
Table of Contents............................................................................................................................................. 3
1. REACTJS ─ OVERVIEW........................................................................................................... 7
Step 6 - index.html......................................................................................................................................... 10
Using JSX........................................................................................................................................................ 13
Attributes ...................................................................................................................................................... 15
Styling ............................................................................................................................................................ 17
3
ReactJS
Comments ..................................................................................................................................................... 18
Validating Props............................................................................................................................................. 32
4
ReactJS
6
ReactJS
1. ReactJS ─ Overview
ReactJS is JavaScript library used for building reusable UI components. According to React
official documentation, following is the definition −
React is a library for building composable user interfaces. It encourages the creation of
reusable UI components, which present data that changes over time. Lots of people use
React as the V in MVC. React abstracts away the DOM from you, offering a simpler
programming model and better performance. React can also render on the server using
Node, and it can power native apps using React Native. React implements one-way
reactive data flow, which reduces the boilerplate and is easier to reason about than
traditional data binding.
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.
React ─ Advantages
Uses virtual DOM which is a JavaScript object. This will improve apps performance,
since JavaScript virtual DOM is faster than the regular DOM.
Can be used on client and server side as well as with other frameworks.
Component and data patterns improve readability, which helps to maintain larger
apps.
React ─ Limitations
Covers only the view layer of the app, hence you still need to choose other
technologies to get a complete tooling set for development.
Uses inline templating and JSX, which might seem awkward to some developers.
7
ReactJS
2. ReactJS ─ Environment Setup
In this chapter, we will show you how to set up an environment for successful React
development. Notice that there are many steps involved but this will help speed up the
development process later. We will need NodeJS, so if you don't have it installed, check
the link from the following table.
Sr.
Software Description
No.
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.
As already mentioned, 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
And lastly, we are setting babel loaders to search for js files, and use es2015 and react
presets that we installed before.
webpack.config.js
var config = {
entry: './main.js',
output: {
path:'./',
filename: 'index.js',
},
devServer: {
inline: true,
port: 8080
},
module: {
9
ReactJS
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
tutorial. 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>
<html lang = "en">
<head>
<meta charset = "UTF-8">
<title>React App</title>
</head>
<body>
<div id = "app"></div>
<script src = "index.js"></script>
</body>
10
ReactJS
</html>
App.jsx
import React from 'react';
We need to import this component and render it to our root App element, so we can see
it in the browser.
main.js
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App.jsx';
Note: Whenever you want to use something, you need to import it first. If you want to
make the component usable in other parts of the app, you need to export it after creation
and import it in the file where you want to use it.
11
ReactJS
C:\Users\username\Desktop\reactApp>npm start
It will show the port we need to open in the browser. In our case, it is
http://localhost:8080/. After we open it, we will see the following output.
12
ReactJS
13