All files / src/chap03 EventOnce.js

0% Statements 0/7
0% Branches 0/2
0% Functions 0/2
0% Lines 0/7

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23                                             
import { useState } from 'react';
 
export default function EventOnce() {
  // 클릭 여부를 관리하기 위한 플래그
  const [clicked, setClicked] = useState(false);
  // 오늘의 운세(점수)
  const [result, setResult] = useState('-');
  const handleClick = e => {
    // 클릭하지 않은 경우에만 운세를 계산한다.
    if (!clicked) {
      setResult(Math.floor(Math.random() * 100 + 1));
      // 플래그 반전
      setClicked(true);
    }
  };
  
  return (
    <>
      <button onClick={handleClick}>결과 보기</button>
      <p>오늘의 운세는 {result}점입니다.</p>
    </>
  );
}