react js (1)
react js (1)
// 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 Array Methods
⮚ There are many JavaScript array methods.
⮚ One of the most useful in React is the .map() array
method.
⮚ The .map() method allows you to run a function on
each item in the array, returning a new array as the
result.
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
❖ The state is an updatable structure that is used to contain data or
information about the component.
❖ The state in a component can change over time. The change in state over
time can happen as a response to user action or system event.
❖ It is the heart of the react component which determines the behavior of the
component and how it will render.
❖ It can be set by using the setState() method and calling setState() method
triggers UI updates.
React State
What is the difference between state and props?
❖ 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
Conditional rendering in React works the same way conditions work in
JavaScript.
There is more than one way to do conditional rendering in React. They
are given below.
● 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} />);