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>
);
}
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.
'Programming > Javascript' 카테고리의 다른 글
자바스크립트 UUID 와 Crypto.randomUUID() 사용하기 (0) | 2023.01.10 |
---|