React Complete
React Complete
// destructuring assignment
var [first, second] = arr;
console.log(first); // Hello
console.log(second); // World
Note:
• When destructuring arrays, the order that variables are declared is
important.
• Notice that the object properties do not have to be declared in a
specific order.
React ES6 Spread Operator
• ES6 introduced a new operator referred to as a spread operator,
which consists of three dots (...).
• The JavaScript spread operator (...) allows us to quickly copy all
or part of an existing array or object into another array or
object.
Syntax
Example
React ES6 Modules
• You can import modules into a file in two ways, based on if they are
named exports or default exports.
• Named exports must be destructured using curly braces. Default
exports do not.
React ES6
• ES6 stands for ECMA Script 6 version
• ECMAScript is the Standardization of Javascript programming
language.
• Use of ES6 features we Write Less and Do More.
React Render HTML
For Example:
ReactDOM.render(<p>Welcome</p>,document.getElementById('root'));
React JSX
• JSX Stands for Javascript XML.
• JSX allows us to write HTML in React.
• JSX allows us to write HTML elements in JavaScript and place them in
the DOM without any createElement().
React JSX
• With JSX code vs Without JSX code
• Expression in JSX
• Wrapping elements or children in JSX
• Styling in JSX
• Attributes in JSX
• Comments in JSX
React Components
React Components
• Every application you will develop in React Will be made up of pieces
called components.
• Components make the task of building UIs much easier.
• We have lot of individual components like a Single web page contain
(Search bar,menu bar,nav bar,content,article etc..,)
• Merge all of these individual components to make a parent
components which will be the final UI
Types of Components
• 1)Function Component
• 2)Class Component
Class Component
function Laptop(props) {
return <h2>I am a { props.brand }!</h2>;
}
ReactDOM.render(<Laptop brand="Dell"
/>,document.getElementById('root'));
Pass Data React Props
• Props are also how you pass data from one component to another, as
parameters.
import React from 'react';
import ReactDOM from 'react-dom';
function Student(props) {
return <h2> { props.name }!</h2>;
}
function College() {
return (
<>
<h1>Who Join today in my Whatsapp Group?</h1>
<Student name="vinitha" />
</>
);
}
ReactDOM.render(<College />,document.getElementById('root'));
React State
❖ props (short for “properties”) and state are both plain JavaScript objects.
❖ While both hold information that influences the output of render, they are
different in one important way:
❖ props get passed to the component (similar to function parameters)
❖ whereas state is managed within the component (similar to variables declared
within a function).
class Reactstate extends React.Component{
constructor()
{
super();
this.state={initalvalue:"Welcome",name:"Guys"}
}
render()
{
return <h1> {this.state.initalvalue} {this.state.name}</h1>
}
}
ReactDOM.render(<Reactstate />,document.getElementById("root"));
class Reactstate extends React.Component{
constructor(){
super();
this.state={initalvalue:"Welcome",name:"Guys"}
}
changevalue = () =>{
this.setState({initalvalue:"Thanks for Watching"})
}
render(){
return <div>
<h1> {this.state.initalvalue} {this.state.name}</h1>
<br/>
<button onClick={this.changevalue} > Exit</button>
</div>
}}
ReactDOM.render(<Reactstate />,document.getElementById("root"));
React Event
❖ An event is an action that could be triggered as a result of the user
action or system generated event
❖ For Example, a mouse click,loading of a web page,pressing a key,
window resizes , and other interactions are calles events.
❖ React events are named using camelCase,rather than lowercase.
❖ In React,with help of JSX you pass a function as the event
handler,rather than a string.
❖ onClick={submit} instead of onclick=”submit()”
import React from 'react';
import ReactDOM from 'react-dom';
function ShowMessage() {
const msg = () => {
alert("Hi all Welcome to ISMUNIV") ;
}
return (
<button onClick={msg}>CLICK!</button>
);
}
ReactDOM.render(<ShowMessage />,document.getElementById('root'));
function ShowMessage() {
const msg = () => {
alert("Hi all Welcome to ISMUNIV") ;
}
const mouseOVer=()=>{
alert("This is MouseOVer");
}
const keyDown=()=>{
alert("This is keyDown");
}
return (
<div>
<button onClick={msg}>CLICK!</button> <br /><br />
<button onMouseOver={mouseOVer}>MouseOver</button><br /><br />
<button onKeyDown={keyDown}>keyDown</button>
</div>
);}
ReactDOM.render(<ShowMessage />,document.getElementById('root'));
React Conditional Rendering
● if
● ternary operator
● logical && operator
function Login() {
return <h1>Welcome To Google</h1>;
}
function NewUser() {
return <h1>Plese sign up</h1>;
}
function Signup(props) {
const isLoggedIn = props.isLoggedIn;
if (isLoggedIn) {
return <Login/>;
}
return <NewUser/>;
}
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<Signup isLoggedIn={true} />);
// index.js
function Temperature(){
return (
<div>
<h1> You Must go hospital</h1>
{
(120 > 103) && alert("High Fever ")
}
</div>
);
}
ReactDOM.render(<Temperature />,document.getElementById('root'));
React Css
★ CSS- Cascading Style Sheet
★ In React we Use to style the React App or Component.
★ Here we are going to discuss mainly three ways to Style react
Components .
1.Inline Styling
2. Css StyleSheet
3. Css Module
Possible Way of Adding CSS to React
Components
Inline Styling:
★ To style an element with the inline style attribute,the value must be
a Javascript object.The Styling must written inside two sets of curly
braces{{}}.
★ If the properties have two names,like background-color,it must be
written in camel case syntax.
CSS Modules:
const root =
ReactDOM.createRoot(document.getElementById('root'));
root.render(<MyForm />);
React Hooks
★ React Hooks allows you to use state and other React features
without writing a class.
★ It does not work inside classes. Its work inside the function only
★ Rules of Hooks:
★ 1. Only call Hooks at the top level
★ Do not call Hooks inside loops,conditions, or nested
functions. Hooks should always be used at the top level of the React
functions.
★ 2.Only call Hooks from React functions
★ You cannot call Hooks from regular Javascript
functions.Instead, you can call Hooks from React function
components.Hooks can also be called from custom Hooks.
// Hook with useState
import React, { useState } from 'react';
import ReactDOM from 'react-dom';
function UIL(){
const [subject,setSubject]=useState("HTML")
return(
<>
<h1> User Interface Language is {subject}!</h1>
<button onClick={()=>setSubject("Cascading Style Sheet")}>
CSS
</button>
<button onClick={()=>setSubject("JAVASCRIPT")}>
Javascript
</button>
<button onClick={()=>setSubject("ReactJS")}>
ReactJS
</button>
</> );}
ReactDOM.render(<UIL/>,document.getElementById('root'));
React Hooks State
★ Hook state is the new way of declaring a state in React app.
★ Hooks uses useState() functional Component for setting and
retrieving state.
★ useState is the Hook which needs to call inside a function
component to add some local state to it.
★ The useState returns a pair where the first eleme
React useReducer Hook
useReducer(<reducer>, <initialState>)
React Router
★ ReactJS Router is mainly used for developing Single Page Web
Applications.
★ React Router is used to define multiple routes in the application.
★ When a user types a specific URL into the browser, and if this URL
path matches any 'route' inside the router file, the user will be
redirected to that particular route.
Need of React Router