programming:frontend:typescript:react:login:contextを使ったシンプルなログイン機能
差分
このページの2つのバージョン間の差分を表示します。
| 次のリビジョン | 前のリビジョン | ||
| programming:frontend:typescript:react:login:contextを使ったシンプルなログイン機能 [2022/06/29 05:46] – 作成 dot | programming:frontend:typescript:react:login:contextを使ったシンプルなログイン機能 [2022/06/29 08:05] (現在) – dot | ||
|---|---|---|---|
| 行 1: | 行 1: | ||
| - | |||
| create-react-app が生成した App.tsx に useContext を使って Login 機能を実装するための概念を説明する雑なソースコード。 | create-react-app が生成した App.tsx に useContext を使って Login 機能を実装するための概念を説明する雑なソースコード。 | ||
| App.tsx | App.tsx | ||
| - | <coce JavaScript> | + | <code JavaScript> |
| import React, { useContext, useState } from ' | import React, { useContext, useState } from ' | ||
| import ' | import ' | ||
| 行 22: | 行 21: | ||
| setUser({account: | setUser({account: | ||
| } | } | ||
| + | }; | ||
| + | const logout = () => { | ||
| + | setUser(null); | ||
| }; | }; | ||
| 行 37: | 行 39: | ||
| </ | </ | ||
| <button onClick={login}> | <button onClick={login}> | ||
| + | <button onClick={logout}> | ||
| < | < | ||
| </ | </ | ||
| 行 48: | 行 51: | ||
| return ( | return ( | ||
| <div> | <div> | ||
| + | { | ||
| + | user ? | ||
| < | < | ||
| + | : | ||
| + | < | ||
| + | } | ||
| </ | </ | ||
| ); | ); | ||
programming/frontend/typescript/react/login/contextを使ったシンプルなログイン機能.1656481562.txt.gz · 最終更新: by dot
