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

reactでuseStateを使用して簡単なカウントシステム実装

Posted at

reactのフックスの学習でuseStateを使用して、ユーザー権限の変更機能を実装したので
まとめます。

①権限をuseStateで定義する。

        変数    右の変数の関数  ()の中は初期値
const [ authority, authorityEdit ] = useState(0)

②カウントアップ、ダウンのボタンを作成。

            <div>
                <button className="btn btn-primary  bg-primary" type="button" onClick={ addition }>権限ダウン</button>
                <button className="btn btn-primary  bg-primary" type="button" onClick={ subtraction }>権限アップ</button>
            </div>

onClickで③で記述する関数が実行されるように記述します。

③、①のstateの値を変更する関数を記述する。(stateの変数に直接変更を加えるのはNG)

    const addition = () => {
        authorityEdit(
            authority +1
        )
    }
    const subtraction = () => {
        authorityEdit(
            authority -1
        )
    }

以上。

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?