useState
useStateとは
state管理
useState() は「React Hooks」の1つで、変数のstate管理するためのファンクション です。state(状態)やprops(プロパティ)が変更されると、リアルタイムにUIが更新されます。この仕組みをリアクティブ(反応的) といいます。
useState定義
インポート
react から、useStateをインポートします。
import React, { useState } from 'react';
変数定義
useState() で「変数」と「更新関数」を定義します。引数には初期値を代入できます。
const [変数, 更新関数] = useState<データ型>(初期値);
useStateの例
変数「count」をuseSateで初期値を「0」として定義します。ボタンをクリックすると「count」が更新され、コンポーネントがリアクティブに更新されます。
useStaet定義
import React, { useState } from 'react';
const CountPage = () => {
// 2) データ更新
const [count, setCount] = useState<number>(0);
return (
<div>
{/* 3) リアクティブ処理 */}
<p>You clicked {count} times<p>
{/* 1) ボタンクリック */}
<button onClick={() => setCount(count + 1)}>
Click me
</button>
</div>
)
}
ユーザ登録ページ
useState追加
「name」「email」「password」を追加します。
app/auth/regist/page.tsx
"use client";
import { useState } from 'react';
function RegistPage() {
const [name, setName] = useState('');
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
...
}
export default RegistPage;
ohChangeイベント
InputコンポーネントのonChangeイベント とvalueを設定をします。
app/auth/regist/page.tsx
function RegistPage() {
const [name, setName] = useState('');
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
return (
<div className="mx-auto w-1/3">
<h1 className="flex p-3 me-3 text-2xl justify-center">
<FaUser className='mt-1 me-3' />
Register
</h1>
<div>
<Input
type="text"
value={name}
placeholder="Your Name"
onChange={setName} />
<Input
type="email"
value={email}
placeholder="Email"
onChange={setEmail} />
<Input
type="password"
value={password}
placeholder="Password"
onChange={setPassword} />
</div>
<div>
<ClickButton label="Sign up" onClick={()=>{}} />
<LinkButton label="Sign in" href="/auth/login" />
</div>
</div>
);
}
export default RegistPage;
Clickイベント
regist() を追加し、テキストボックスのデータをコンソール表示します。
app/auth/regist/page.tsx
"use client";
import { useState } from 'react';
function RegistPage() {
const [name, setName] = useState('');
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
const regist = async () => {
console.log(name, email, password);
}
...
}
export default RegistPage;
Clickイベントに regist() を追加します。
app/auth/regist/page.tsx
"use client";
import { useState } from 'react';
function RegistPage() {
const [name, setName] = useState('');
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
const regist = async () => {
console.log(name, email, password);
}
return (
<div className="mx-auto w-1/2">
...
<div>
<ClickButton label="Sign up" onClick={regist} />
<LinkButton label="Sign in" href="/auth/login" />
</div>
</div>
);
}
export default RegistPage;
動作確認
フォームにデータを入力して、ブラウザコンソールに表示されるか確認します。
結果
Tokyo [email protected] password
未入力チェック
テキストボックスにデータがすべて入力されるまで、ClickButtonを無効にします。
isDisabled
isDisabled() で「email」「email」「password」の未入力をチェックします。
app/auth/regist/page.tsx
function RegistPage() {
...
//未入力チェック
const isDisabled = () => !(name && email && password);
...
}
export default RegistPage;
ボタン無効
ClickButtonの「disabled」プロパティでボタンの有効・無効を設定します。
app/auth/regist/page.tsx
function RegistPage() {
...
const isDisenabled = () => {
return !(name && email && password)
}
return (
<div className="mx-auto w-1/3">
...
<div>
<ClickButton label="Sign up" onClick={regist} disabled={isDisabled()} />
<LinkButton label="Sign in" href="/auth/login/" />
</div>
</div>
);
}
export default RegistPage;
ボタン有効・無効確認
データが入力されないと、【Sigin up】ボタンが無効になります。
結果
ログインページ
useState追加
「email」「password」「error」を追加します。
app/auth/login/page.tsx
"use client";
import { useState } from 'react';
const LoginPage = () => {
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
const [error, setError] = useState<Error>({ auth: "" });
...
}
ohChangeイベント
InputコンポーネントのonChangeイベント とvalueを設定をします。
app/auth/login/page.tsx
const LoginPage = () => {
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
const [error, setError] = useState<Error>({ auth: "" });
return (
<div className="mx-auto w-1/3">
<h1 className="flex p-3 me-3 text-2xl justify-center">
<RiLockPasswordFill className='mt-1 me-3' />
Sign in
</h1>
<div>
<Input
type="text"
value={name}
placeholder="Your Name"
onChange={setName} />
<Input
type="email"
value={email}
placeholder="Email"
onChange={setEmail} />
<Input
type="password"
value={password}
placeholder="Password"
onChange={setPassword} />
</div>
...
</div>
);
}
export default LoginPage;
Clickイベント
auth() を追加し、テキストボックスのコンソール表示します。
app/auth/login/page.tsx
"use client";
import { useState } from 'react';
function LoginPage() {
const [name, setName] = useState('');
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
const auth = async () => {
console.log(email, password);
}
...
}
Clickイベントに auth() を追加します。
app/auth/regist/page.tsx
function LoginPage() {
...
return (
<div className="mx-auto w-1/2">
...
<div>
<ClickButton label="Sign in" onClick={auth} />
<LinkButton label="Register" href="/auth/regist/" />
</div>
</div>
);
}
export default LoginPage;
動作確認
フォームにデータを入力して、ブラウザコンソールに表示されるか確認します。
結果
[email protected] password
未入力チェック
テキストボックスにデータがすべて入力されるまで、ClickButtonを無効にします。
isDisenabled
isDisabled() で「email」「password」の未入力をチェックします。
app/auth/regist/page.tsx
function LoginPage() {
...
//未入力チェック
const isDisabled = () => !(email && password);
...
}
export default RegistPage;
ボタン無効
ClickButtonの「disabled」プロパティでボタンの有効・無効を設定します。
app/auth/regist/page.tsx
const LoginPage = () => {
...
return (
<div className="mx-auto w-1/3">
<h2 className="flex p-3 me-3 text-2xl justify-center">
<RiLockPasswordFill className='mt-1 me-3' />
Sign in
</h2>
<div>
<Input
type="email"
value={email}
placeholder="Email"
onChange={setEmail}
/>
<Input
type="password"
value={password}
placeholder="Password"
onChange={setPassword}
/>
<FormError message={error.auth} />
</div>
<div>
<ClickButton label="Sign in" onClick={auth} disabled={isDisabled()} />
<LinkButton label="Register" href="/auth/regist/" />
</div>
</div>
);
}
export default LoginPage;
ボタン有効・無効確認
データが入力されないと、【Sigin in】ボタンが無効になります。
結果
ローディング
React Spinners
ローディングは非同期処理中に待機状態を表示するUIで、UI/UXの向上をさせます。アイコンは「React Spiners」を利用すると便利です。
React Spinersインストール
npmで「React Spinners」をインストールします。
npm i react-spinners
Loadingコンポーネント作成
「app/components/Loading.tsx」を作成し、「ClipLoader」コンポーネントを表示します。
app/components/Loading.tsx
import { ClipLoader } from 'react-spinners';
const Loading = () => {
return (
<div className="flex justify-center items-center m-5">
<ClipLoader size={40} color="#2ab393" />
</div>
)
}
export default Loading;
Loadingの表示
useState
ログインページに「isLoading」を追加し、auth() を実行した時に「true」にします。
app/components/Loading.tsx
...
const LoginPage = () => {
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
const [error, setError] = useState<Error>({ auth: "" });
// isLoadig, setIsLoadig 追加
const [isLoadig, setIsLoadig] = useState(false);
const auth = async () => {
console.log(email, password);
setIsLoadig(true);
}
...
}
export default LoginPage;
Loadingコンポーネント表示
「isLoading」に応じてLoadingコンポーネントを表示します。
app/components/Loading.tsx
...
return (
<div className="mx-auto w-1/3">
...
{(isLoadig) ?
<Loading />
:
<div>
<ClickButton label="Sign in" onClick={auth} disabled={isDisabled()} />
<LinkButton label="Register" href="/auth/regist/" />
</div>
}
</div>
);
...
ローディング確認
【Sign in】ボタンをクリックして、ローディングが表示されるか確認します。

コース内レッスン
- 1
Lesson 1
はじめるまえに
レッスンを見る - 2
Lesson 2
プロジェクト作成 - Laravel -
レッスンを見る - 3
Lesson 3
API処理 1
レッスンを見る - 4
Lesson 4
Postman
レッスンを見る - 5
Lesson 5
ユーザ認証API
レッスンを見る - 6
Lesson 6
API処理 2
レッスンを見る - 7
Lesson 7
プロジェクト作成 - Next.js -
レッスンを見る - 8
Lesson 8
レイアウト
レッスンを見る - 9
Lesson 9
コンポーネントの基本1
レッスンを見る - 10
Lesson 10
コンポーネントの基本2
レッスンを見る - 11
Lesson 11
コンポーネントの基本3
レッスンを見る - 12
Lesson 12
ルーティング
レッスンを見る - 13
Lesson 13
useState
レッスンを見る - 14
Lesson 14
モデルとサービス
レッスンを見る - 15
Lesson 15
useEffect
レッスンを見る - 16
Lesson 16
useRouter
レッスンを見る - 16
Lesson 16
プロパティ(props)
レッスンを見る - 17
Lesson 17
Cookie
レッスンを見る - 18
Lesson 18
useContext
レッスンを見る - 19
Lesson 19
NextAuth
レッスンを見る - 20
Lesson 20
NextAuthで認証
レッスンを見る