React JS 1
React JS 1
Front-end library used for developing code to create GUI for web
application
Created by Jordan Walke, a software engineer at Facebook
Open sourced to the world by Facebook and Instagram.
Key Features
React Angular
React is a small view library Angular is a full framework
React covers only the
Angular provides the complete solution
rendering and event handling
for front-end development
part
Presentation code in Presentation code in HTML embedded
JavaScript powered by JSX with JavaScript expressions
React's core size is smaller Angular being a framework contains a lot
than Angular, so bit fast of code, resulting in longer load time
React is very flexible Angular has less flexibility
Great performer, since it uses Angular uses actual DOM which affects its
Virtual DOM performance
React JS Installation
Need create-react-app tool. This is available as part of Node js
Install Node Js using the following link
https://nodejs.org/en/download/
After installation, go to the terminal of visual code and type the
following command
node –v
npx create-react-app my-app
Or
D:\>npm install -g create-react-app
D:\>create-react-app my-app
D:/>my-app>npm start
Why Components?
We have the following challenges in developing this application:
1. Entire page will get re-rendered even when a section of the page (eg.
feedback section) undergoes changes
2. We will have to re-write code for each item even though they have similar
behavior
Creating a Component
App.js
index.js
Let us see how to write the same above Login component using JSX in a
easier way:
class App extends React.Component{
render(){
return (<form><h2>Login</h2>
<input type="text" placeholder="Name" /><br/><br/>
<input type="password" placeholder="password" />
<br/><br/>
<input type="submit" value="log" />
</form>);
}
};
export default App;
JSX is a special syntax introduced in ReactJS to write elements
of components. It is syntactically identical to HTML and hence it can
be easily read and written. Code written using JSX helps in visualizing
the DOM structure easily.
class App extends React.Component {
render() {
return <h1> Hello World </h1>
}
}
export default App;
As the browser does not understand JSX code, this gets converted to JavaScript using the
plugins of the babel.
<React.Fragment>
React Fragments allow you to wrap or group multiple elements
without adding an extra node to the DOM. This can be useful when
rendering multiple child elements/components in a single parent
component.
}
}
{}
JavaScript expressions to be evaluated should be wrapped within curly
braces as follows:
Accessing a variable:
We can access the value of any variable within curly braces as shown
below:
class App extends React.Component {
render() {
let count = 5
return(<React.Fragment>
<h1>{count}</h1>
<h2>{count*count}</h2>
</React.Fragment>)
}}
Arithmetic operators can also be used as shown above
Accessing an object:
Conditional Rendering
render() {
let element = null;
let isLoggedIn = false
if(isLoggedIn) {
element = <h2>Welcome Admin</h2>
}
else {
element = <h2>Please Login</h2>
}
return (<React.Fragment>
{element}
</React.Fragment>)
}
}
export default App;
return (<React.Fragment>
<table style={{width:'60%'}} className='table'>
<thead className="thead-light">
<tr>
<th>EmpID</th>
<th>Name</th>
<th>Designation</th>
</tr>
</thead>
<tbody>
{employees.map(employee => {
return (<tr>
<td>{employee.empId}</td>
<td>{employee.name}</td>
<td>{employee.designation}</td>
</tr>)
})
}
</tbody>
</table>
</React.Fragment>)
}
}
export default App;
Error
<tbody>
{ employees.map(employee => {
return (<tr key={employee.empId}>
<td>{employee.empId}</td>
<td>{employee.name}</td>
<td>{employee.designation}</td>
</tr>)
})
}
</tbody>
Another Example
import React from 'react';
import 'bootstrap/dist/css/bootstrap.min.css';
class App extends React.Component {
render() {
var employees = [
{ empId: 1234, name: 'Jack', designation:'SE', salary:23000},
{ empId: 4567, name: 'Johnson', designation:'SSE',
salary:15000},
{ empId: 8910, name: 'Sachin',designation:'TA', salary:30000}
]
return (<React.Fragment>
<table style={{width:'60%'}} className='table'>
<thead className="thead-light">
<tr>
<th>EmpID</th>
<th>Name</th>
<th>Designation</th>
</tr>
</thead>
<tbody>
{employees.map(employee => {
return employee.salary > 15000 ? (<tr
key={employee.empId}>
<td>{employee.empId}</td>
<td>{employee.name}</td>
<td>{employee.designation}</td>
</tr>) : null
})
}
</tbody>
</table>
</React.Fragment>)
}
}
export default App;
.button {
background-color:blue;
color:white;
border-radius:10px;
width:100px;
height:30px;
}
import './App.css';
<button className="button">Submit</button>
import the bootstrap CSS file within the AppComp component and
apply the bootstrap btn btn-success class to the button as shown below
import 'bootstrap/dist/css/bootstrap.min.css';
<button className="btn btn-success">Submit</button>
If you are using the js file of Bootstrap then import the bootstrap js file
but before that install jQuery, as the bootstrap JavaScript depends on
jQuery
What is State?
The state is an initial value set for a component, which is used for
interactivity.
Syntax:
constructor() {
super();
this.state = { counter: 1 };
In the above code, the state 'counter' is set to 1 and 'counter' would be accessed
as this.state.counter.
this.handleClick=this.handleClick.bind(this)
}
handleClick(e){
this.setState({counter:this.state.counter+1})
}
render() {
return(<React.Fragment>
<h2> Seconds Elapsed: {this.state.counter} </h2>
<button onClick = {this.handleClick}> Increment Counter </button>
</React.Fragment>)
}
}
export default Timer;
update = () => {
this.setState({quantity:5},()=>{
if(this.state.quantity == 5) {
this.setState({price:2000-100})
}
})
}
</div>
)
}
<Pass2 name="V.Mohanraj"/>
function Pass2(props)
{
return(
<h1> {props.name}</h1>
)
}
export default Pass2;
// Parent Component
class Parent extends React.Component{
render(){
return(
<div>
<h2>You are inside Parent
Component</h2>
<Child name="User" userId =
"5555"/>
</div>
);
}
}
// Child Component
class Child extends React.Component{
render(){
console.log(this.props);
return(
<div>
<h2>Hello,
{this.props.name}</h2>
<h3>You are inside Child
Component</h3>
<h3>Your user id is:
{this.props.userId}</h3>
</div>
);
}
}
ReactDOM.render(
// passing props
<Parent />,
document.getElementById("root")
);
import React from 'react';
constructor()
{
super();
this.state={counter:0};
this.Incr=this.Incr.bind(this);
this.Decr=this.Decr.bind(this);
}
Incr(e) {
this.setState({counter:this.state.counter+1})
}
Decr(e){
console.log("Inside decr ")
this.setState({counter:this.state.counter-1})
}
render()
{
return(
<div className='container-fluid'>
<h1 className='text-primary'> Counter Value:
{this.state.counter}</h1>
</div>
)
}
}
constructor()
{
super();
this.state={counter:0,mobile:9786123706};
this.Incr=this.Incr.bind(this);
this.Decr=this.Decr.bind(this);
this.setMobile=this.setMobile.bind(this);
}
Incr(e) {
this.setState({counter:this.state.counter+1})
}
Decr(e){
console.log("Inside decr ")
this.setState({counter:this.state.counter-1})
}
setMobile(e)
{
let m=this.props.mobile;
this.setState({mobile:m},()=>{
console.log(this.state.mobile);
})
}
render()
{
return(
<div className='container-fluid'>
<h1 className='text-primary'> Counter Value:
{this.state.counter}</h1>
<input type="button" className="btn btn-info" value="Set
Mobilenumber" onClick={this.setMobile}/>
</div>
)
}
<link
href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
rel="stylesheet">
<script
src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min
.js"></script>
Commenting a react component
{/* <componentname/>*/}