Today I have learned Hooks in javascript
Hooks in javascript:
Hooks were added to React in version 16.8.
Hooks allow function components to have access to state and other React features. Because of this, class components are generally no longer needed.
What is a Hook?
Hooks allow us to "hook" into React features such as state and lifecycle methods.
Hook Rules
There are 3 rules for hooks:
- Hooks can only be called inside React function components.
- Hooks can only be called at the top level of a component.
- Hooks cannot be conditional
Note: Hooks will not work in React class components.
increment=()=>{
this.setState((login)=>({
count:login.count+1
}))
}
decrement=()=>{
this.setState((login)=>({
count:login.count-1
}))
if(this.state.count<1){
alert("Less than 0");
this.setState((a)=>({
count:a.count+1
}))
}
}
render(){
return(
<div>
<h1>Incerment using hooks</h1>
<p>count:{this.state.count}</p>
<button onClick={this.increment}>Incerment</button>
<br></br>
<h1>Decrement using hooks</h1>
<p>count:{this.state.count}</p>
<button onClick={this.decrement}>Decrement</button>

.png)
Comments
Post a Comment