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を使ってみる ~ ReactHooks/useState ~

Last updated at Posted at 2020-11-28

はじめに

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

クリックすると数字が増えていくというやつです。

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

const Counter = () => {
  const [count, setCount] = useState(0);

  return(
    <div>
       <p>count: {count}</p>
       {/* onClick={} の中に関数を書くとその関数が実行されます。*/}
       <button onClick={() => setCount(count + 1) }>click!</button>   
    </div>
  );
}

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

スクリーンショット 2020-11-29 0.54.17.png

表示成功(静止画を表示してますがクリックすると数が増えました)

まとめ

  • ファイルの先頭に import React, { useState } from 'react'; を書くと useStateが使えるようになる。
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?