React 6
React 6
========================
Action to which javascript response is called event handling.
Ex:
JavaScript
-----------
<button onclick="f1()"> clickme </button>
ReactJS
--------
Function component : <button onClick={handleClick}> clickme
</button>
Class component : <button onClick={this.handleClick}> clickme
</button>
myapp10
|
|---node_modules
|
|---public
| |
|---index.html
|---manifest.json
|---favicon.ico
|----src
| |
|---index.js
|---App.js
|
|---package.json
|---READMD.md
step1:
-----
Create react application i.e myapp10.
ex:
Reactprojects> npx create-react-app myapp10
step2:
------
Open VSC editor.
ex:
Reactprojects> code .
step3:
------
Switch to the react project.
ex:
Reactprojects> cd myapp10
step5:
------
Add "web-vital" dependency.
ex:
Reactprojects/myapp10> npm install web-vitals
step6:
-----
Run the react application.
ex:
Reactprojects/myapp10> npm start
step7:
-----
Write a below code inside App.js file.
App.js
-------
import React from 'react'
function App() {
const handleClick=()=>
{
console.log("You have clicked")
}
return (
<div>
<button onClick={handleClick}> click Me </button>
</div>
)
}
or
App.js
-------
import React from 'react'
function App() {
const handleClick=(e)=>
{
e.preventDefault();
}
return (
<div>
<a href="http://www.google.com" onClick={handleClick}> click Me </a>
</div>
)
}
|----src
| |
|---index.js
|---App.js
|
|---package.json
|---READMD.md
step1:
-----
Create react application i.e myapp10.
ex:
Reactprojects> npx create-react-app myapp10
step2:
------
Open VSC editor.
ex:
Reactprojects> code .
step3:
------
Switch to the react project.
ex:
Reactprojects> cd myapp10
step5:
------
Add "web-vital" dependency.
ex:
Reactprojects/myapp10> npm install web-vitals
step6:
-----
Run the react application.
ex:
Reactprojects/myapp10> npm start
step7:
-----
Write a below code inside App.js file.
App.js
-------
import React, { Component } from 'react'
render() {
return (
<div>
<button onClick={this.handleClick}> click Me </button>
</div>
)
}
}
myapp11
|
|---node_modules
|
|---public
| |
|---index.html
|---manifest.json
|---favicon.ico
|----src
| |
|---index.js
|---App.js
|
|---package.json
|---READMD.md
step1:
-----
Create react application i.e myapp11.
ex:
Reactprojects> npx create-react-app myapp11
step2:
------
Open VSC editor.
ex:
Reactprojects> code .
step3:
------
Switch to the react project.
ex:
Reactprojects> cd myapp11
step5:
------
Add "web-vital" dependency.
ex:
Reactprojects/myapp11> npm install web-vitals
step6:
-----
Run the react application.
ex:
Reactprojects/myapp11> npm start
step7:
-----
Write a below code inside App.js file.
App.js
------
import React, { Component } from 'react'
state={
name:"Alan",
rollno:101
}
handleClick=()=>
{
this.setState({"name":"Jose","rollno":201});
}
render() {
return (
<div>
<h1>Name : {this.state.name}</h1>
<h1>Roll No : {this.state.rollno}</h1>
props state
-------------- -------------
It is read-only. It is updatable.
It is immutable. It is mutable.
1)Mounting
-----------
Mounting is a process of creating an element and inserting it in a DOM tree.
2)Updating
-------------
Updating is a process of changing state or props of a component and update changes
to nodes already existing in the DOM.
3)Error Handling
----------------
Error Handling used when there is a error during rendering ,in lifecycle
method or in the constructor of any child component.
4)Unmounting
---------------
Unmounting is a process of removing elements from the DOM.
In general it will clear the reserved memory.
1) componentWillUnmount()