1
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 1 year has passed since last update.

React初心者がやるReact Hooks -useState編-

Last updated at Posted at 2023-02-11

アウトプットのために簡単なカウンターアプリをReact HooksのuseStateを使用して作成する

プロジェクト作成

$ npx create-react-app hooks-tutorial --template typescript

Counter.tsxの作成

Counter.tsx
import React, { useState } from 'react';

const Counter: React.FC = () => {
  const [count, setCount] = useState<number>(0);
  return (
    <>
      <h1>{count}</h1>
      <button onClick={() => setCount((prev) => prev + 1)}>+</button>
      <br />
      <button onClick={() => setCount((prev) => 0)}>RESET</button>
    </>
  );
};

export default Counter;

App.tsxへインポート

App.tsx
import React from 'react';
import Counter from './components/Counter';

const App: React.FC = () => {
  return (
    <div className="App">
      <Counter />
    </div>
  );
};

export default App;

カウンターアプリの完成

スクリーンショット 2023-02-11 16.34.10.png

1
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
1
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?