React hooks are a feature in React that allow you to add state and other React features to functional components. Here is an example of the useState hook, which allows you to add state to a functional component:

import React, { useState } from 'react';

function Example() {
  // Declare a state variable named "count"
  const [count, setCount] = useState(0);

  return (
    <div>
      <p>You clicked {count} times</p>
      <button onClick={() => setCount(count + 1)}>
        Click me
      </button>
    </div>
  );
}
 
In this example, the useState hook is used to declare a state variable named count. The hook returns an array with two elements: the current value of the state, and a function that can be used to update the state. The value of count is displayed in a p element, and a button is provided that, when clicked, will increment the value of count.
 
 
This simple example demonstrates the basic use of the useState hook and how it can be used to add state
to a functional component in React

 

Summary:

This demonstrates the use of the useState hook in React. The useState hook allows you to add a state to a functional component, making it easier to manage the state of your React application. The example provided shows the declaration of a state variable named count using the useState hook and the use of this state in a functional component. The value of count is displayed in a p element and updated when a button is clicked, showcasing the basic functionality of the useState hook.

+ Recent posts