11 Reactjs Notes
11 Reactjs Notes
useEffect hook
useReducer hook
useCallback hook
Capstone Project Frontend (Guideline only)
================================================================
useEffect Hook
================================================================
- Consider the requirement
- provide custom title to a page instead of 'React App'
- title should be button clicked 'clicked x times'
- this is done by two lifecycle methods, componentDidMount() and
componentDidUpdate().
- Consider this class component code
Conditional rendering
================================================================
useReducer
================================================================
—----------------------------------------------------
================================================================
useCallback hook (Performance Optimization Eg01)
================================================================
- See the performance issue in the code
- here we passed functions as props.
Directory structure.
useCallback
- ParentCompo.js
- Title.js
- Count.js
- Button.js
***Title.js***
import React from "react"
function Title() {
console.log('Rendering Title')
return (
<h2>
useCallBack Hook
</h2>
)
}
//export default Title
//////////01
export default React.memo(Title)
***Count.js***
import React from "react"
function Count({ text, count }) {
console.log(`Redering ${text}`)
return <div>{text} - {count} </div>
}
//export default Count
//////////01
export default React.memo(Count)
***Button.js***
import React from "react"
function Button({ handleClick, children }) {
console.log('Rendering Button - ', children)
return (
<button onClick={handleClick}>
{children}
</button>
)
}
//export default Button
//////////01
export default React.memo(Button)
***ParentCompo.js***
import { useCallback, useState } from 'react'
import Title from './Title'
import Count from './Count'
import Button from './Button'
function ParentCompo() {
const [age, setAge] = useState(25)
const [salary, setSalary] = useState(50000)
/*//////////02
const incrementAge = () =>{
setAge(age + 1)
}
Note:-
1. Here if we observe the console, at each button click all components
are rendering.
2. To limit this rendering use React.memo
3. Now spot the issue, still there is rerendering of Button component
4. Again to optimise this we will use the 'useCallback' hook.
5. it calls function only if the dependent value i.e. second argument to
useCallback changes.
6. Now the issue is solved.
***url.js***
module.exports = "http://localhost:8080"
***aboutus.js***
import React from 'react'
export default class Aboutus extends React.Component{
render(){
return(
<div className='container mt-5'>
<p className='jumbotron'>Welcome to Aboutus</p>
</div>
)
}
}
***SignupComponent.js***
import React from 'react'
import axios from 'axios'
import url from './url'
export default class SignupComponent extends React.Component {
constructor() {
super()
this.state = {
status:''
}
}
render() {
return (
<div className='container mt-5'>
<form onSubmit={this.signup} className='btn
btn-outline-warning w-50'>
<h3 className='text-primary'>Signup user </h3>
<div className='form-group my-2 btn btn-outline-dark p-3
w-100'>
<label>User id</label>
<input type='text' placeholder='Enter Userid'
className='form-control' name='userid'></input>
</div>
<div className='form-group my-2 btn btn-outline-dark p-3
w-100'>
<label>User Name</label>
<input type='text' placeholder='Enter User Name'
className='form-control' name='uname'></input>
</div>
<div className='form-group my-2 btn btn-outline-dark p-3
w-100'>
<label>Password</label>
<input type='password' placeholder='Enter Password'
className='form-control' name='upwd'></input>
</div>
<div className='form-group my-2 btn btn-outline-dark p-3
w-100'>
<label>User email</label>
<input type='email' placeholder='Enter User email'
className='form-control' name='email'></input>
</div>
<div className='form-group my-2 btn btn-outline-dark p-3
w-100'>
<label>User Address</label>
<input type='text' placeholder='Enter User Address'
className='form-control' name='address'></input>
</div>
<div className='form-group my-2 btn btn-outline-dark p-3
w-100'>
<label>Contact</label>
<input type='text' placeholder='Enter Contact'
className='form-control' name='contact'></input>
</div>
<div className='form-group my-2 w-25 mx-auto' align =
'center'>
<input type='submit' className='btn
btn-outline-success' value='Signup'></input>
<h3>{this.state.status}</h3>
</div>
</form>
</div>
)
}
signup = (e) => {
e.preventDefault()
let obj = {
"userid" : e.target.userid.value,
"uname" : e.target.uname.value,
"upwd" : e.target.upwd.value,
"email" : e.target.email.value,
"address" : e.target.address.value,
"contact" : e.target.contact.value
}
axios.post(url+"/insert/createUser",obj)
.then((posRes)=>{
console.log(posRes.data)
this.setState({
status : posRes.data.userInsert
})
},(errRes)=>{
console.log(errRes)
})
}
}