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.
It is widely used for building user interfaces for web
applications where user interactions are dynamic and frequent.
React allows developers to create reusable UI components,
making the development process more modular and efficient.
Why React JS?
Creating web apps that handles huge amount of data
Creating Single page web Application
Creating reusable GUI component
Code to be written for manipulating DOM tree after user clicking the like
button and re-render the web page in a browser
Above activity should be reflected in other users too.
Considering the application size of Facebook which has more than 1.6
billion daily users and 500,000 “likes” every minutes, this was a
challenge to the engineers of Facebook.
Key Features
JSX syntax: React used JSX syntax which is similar to XML and HTML
syntax which makes it easy for writing markup and binding events in
components
- JSX stands for JavaScript XML. JSX allows us to write HTML in
React.
- JSX makes it easier to write and add HTML in React
React Angular
React is a small view library Angular is a full framework
React covers only the rendering Angular provides the complete solution
and event handling part for front-end development
Presentation code in JavaScript Presentation code in HTML embedded with
powered by JSX JavaScript expressions
React's core size is smaller than Angular being a framework contains a lot of
Angular, so bit fast 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@latest my-app
The above command creates a project my-app with the below folder
structure:
Files Purpose
All the node module dependencies are created in this
node_modules
folder (babel)
This folder contains the public static assets of the
public
application
public/ First page that is rendered on the browser or [age
index.html template
src All application related files/folders are created in this folder
src/index.js Entry point of the application
package.json Contains the dependencies of the React application
D:/>my-app>npm start
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
3. We will have to take additional care to make sure that the functionality of one
part of the application do not interfere with another part
App.js
index.js
function App() {
return React.createElement(
'form', {},
React.createElement('h1', {}, 'Login'),
React.createElement('input', { type: 'text', placeholder: 'Name',
value: '' }),
React.createElement('br', {}),
React.createElement('br', {}),
React.createElement('input', { type: 'password', placeholder:
'Password', value: '' }),
React.createElement('br', {}),
React.createElement('br', {}),
React.createElement('button', { type: 'submit' }, 'Login')
);
}
As the browser does not understand JSX code, this gets converted to JavaScript using
the plugins of the babel.
React.Fragment is almost always the better choice to reduce the overall DOM size.
}
}
Javascript Expression in JSX
We discussed how to use JSX to create React elements, we may also require
to use JavaScript expressions in React elements, so let's see how to write
JavaScript expressions in JSX.
{}
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>)
}}
Accessing an object:
Conditional Rendering
class App extends React.Component {
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;
<h1 style={{color:'green',fontFamily:'verdana'}}>Welcome to
React</h1>
.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.
Using constructor, the state of a component is created and initialized using this.state as
follows:
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;
They are reserved only for interactivity and component's event handlers may
change to trigger a UI update
The state will be set with a default value when component mounts and will mutate
in time based on user events generated
Props
When we want to pass any data from one component to another component, it is passed
as a prop.
function Pass2(props)
{
return(
<h1> {props.name}</h1>
)
}
export default Pass2;
ReactDOM.render(
// passing props
<Parent />,
document.getElementById("root")
);
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>
</div>
)
}
<link
href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/boots
trap.min.css" rel="stylesheet">
<script
src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstr
ap.bundle.min.js"></script>
{/* <componentname/>*/}