0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【React】state を使ってみる ~ React Hooks/useEffect ~

Last updated at Posted at 2020-11-28

はじめに

React を習得するまでの軌跡をメモっていく備忘録的な記事です。

state を使って1秒毎にカウントアップするタイマーを作る

index.js
import React, { useState, useEffect } from 'react';
import ReactDOM from 'react-dom';

const Timer = () => {
  const [time, setTime] = useState(0);

  // クリーンアップ関数を登録(return)する
  useEffect(() => {
    const timerId = setInterval(() => setTime(time + 1), 1000);
    return () => clearInterval(timerId);
  });

  return(
    <p>
      {time}
    </p>
  )
};

ReactDOM.render(
    <Timer />,
  document.getElementById('root')
);

まとめ

import React, { useEffect } from 'react';を書くとuseEffectが使えるようになる。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?