Skip to content

Latest commit

 

History

History
40 lines (16 loc) · 860 Bytes

React Hooks.md

File metadata and controls

40 lines (16 loc) · 860 Bytes

React Hooks

r

React Hooks are a way to add state and other React features to functional components. Before hooks, you had to use class components to use state, but now you can use hooks in functional components. Here's an example of using the useState hook to create a counter:

import React, { useState } from 'react';

function Counter() {

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

return (

<div>

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

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

    Click me

  </button>

</div>

);

}

In this code, useState is a hook that takes an initial value (0 in this case) and returns an array with the current state value (count) and a function to update the state (setCount).