Reactjs Hooks With State Example

React.js

Nicesnippets

80

23-01-2020


Hello Guys,

Today we are learing about reactjs hooks. hooks introduce in react js 16.8 varsion. you can use hooks in above 16.8 varsion in reactjs. They let you use state and other React features without writing a class component. you can also use life cycle method using hooks. hooks only work in functional component it not work in class component.

Hooks With State Example

In following example you can see first you have to use state in component first you hav to defin in import like "useState". then you have to define variable then you can use.

/src/App.js file

import React,{useState} from 'react';

function App(){

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

return(

<div>

<p>You clicked {count} times</p>

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

</div>

)

}

export default App;

I hope it can help you...


Recommended Posts