React useEffect Hooks

유관희·2022년 5월 29일
0
post-thumbnail
post-custom-banner

useEffect accepts two arguments. The second argument is optional.

useEffect(<function>, <dependency>)

Example:

import { useState, useEffect } from "react";
import ReactDOM from "react-dom/client";

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

  useEffect(() => {
    setTimeout(() => {
      setCount((count) => count + 1);
    }, 1000);
  });

  return <h1>I've rendered {count} times!</h1>;
}
  1. No dependency passed:
useEffect(() => {
  //Runs on every render
});코드를 입력하세요
  1. An empty array:
useEffect(() => {
  //Runs only on the first render
}, []);
  1. Props or state values:
useEffect(() => {
  //Runs on the first render
  //And any time any dependency value changes
}, [prop, state]);
profile
안녕하세요~
post-custom-banner

0개의 댓글