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.

Universally Unique Identifier (UUID) 는 리소스들 중에서 리소스를 유니크하게 식별하기 위한 식별자 이다. Javscript 에서 UUID를 생성하기 위한 방법으로 uuid 모듈을 사용하는 방법과 crypto에서 제공하는 raddomUUID() 함수를 사용하는 방법이 있다. 

 

1. UUID 모듈을 사용하여 UUID 생성하기.

 

// 설치하기
npm install uuid

// UUID 생성하기 (ES6 syntax)
import { v4 s uuidV4 } from "uuid"
console.log(uuidV4()); => '9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d'

위에 코드와 같이 랜덤 UUID를 생성할 수 있다, 하지만 uuidV4() 함수를 호출 하기 위해서는 "uuid" 라이브러리를 항상 import 해주어야 하는데, Crypto 라이브러리에 randomUUID() 함수를 사용하게 되면 이러한 부분을 생략할 수 있다.

 

2. Crypto 모듈을 사용하여 UUID 생성하기

console.log(crypto.randomUUID());​

https://developer.mozilla.org/en-US/docs/Web/API/Crypto/randomUUID

 

 

Crypto.randomUUID() - Web APIs | MDN

The randomUUID() method of the Crypto interface is used to generate a v4 UUID using a cryptographically secure random number generator.

developer.mozilla.org

 

'Programming > Javascript' 카테고리의 다른 글

Introducing React Hooks  (0) 2023.02.10

+ Recent posts