Hooks in React.js

 Hooks:-


What is hooks :- hooks is a special function that is used in the function component for handling state , api call , async promises handling and all this feature which you handle in the class component only now using hooks we can handle all this feature.


When would I use a hook? : if you write a function component and you realize you need to add some state to it , previously you need to convert it into a class component but with the help of hooks you can easily handle state in the function component.


Example:- useState is a hook that use to add the state in function component 


Here is an example of how the hooks are working 



import "./styles.css";

import React, { useState } from "react";

 

export default function App() {

 const [count, setCount] = useState(0);

 return (

   <div className="App">

     <p>you click {count} time </p>

     <button onClick={() => setCount(count + 1)}>click me</button>

   </div>

 );

}


Output:-


In this above example you can click the button and change the count 


Code Link:-


Post a Comment

0 Comments