
Expo(React Native) - )ログイン案内モーダルの実装
概要
ログインが必要なアプリの特定機能にアクセスする時、勝手にログインページにルーティングさせるのはUXとしては良くないと思います。
ユーザーに十分な情報を与え、ログインページに誘導する必要があります。
今回は
1)ログイン誘導モーダル(RouteAuthModal) を表示し、
2)「ログイン」ボタン押下時に現在のパスを保存して、ログイン後に元のページへリダイレクトする
という仕組みを実装しました。
プルジェクト情報
技術スタック(Frontend)
Expo
Zustand
React Native Modal
TypeScript
フォルダ構成
app
- (modals)
-- routeAuth.tsx (ログイン案内モーダル)
- (tabs)
-- community
--- list.tsx (サンプルページ)
- _layout.tsx (ルートレイアウト)
store
- common
-- useAuthModalStore.ts (モーダル表示制御系グローバル状態)
-- useRouteStore.ts (Redirect関連系グローバル状態)仕組み
0)全体コード
routeAuth.tsx
import { USER_STATUS_INCOMPLETE } from "@/constants/const";
import { useAuthModalStore } from "@/store/common/useAuthModalStore";
import { useRouteStore } from "@/store/common/useRouteStore";
import { useUserStore } from "@/store/user/useUserStore";
import { usePathname, useRouter } from "expo-router";
import { Modal, Pressable, StyleSheet, Text, View } from "react-native";
type RouteAuthModalProps = {
isVisible: boolean;
};
export default function RouteAuthModal({ isVisible }: RouteAuthModalProps) {
const router = useRouter();
const pathname = usePathname();
const { setRedirectTo } = useRouteStore();
const { id, status } = useUserStore();
const { onAuthModalClose } = useAuthModalStore();
const handleLoginButtonPress = () => {
try {
setRedirectTo(pathname);
if (!id) {
return router.push("/(auth)/sign/phone");
}
if (status === USER_STATUS_INCOMPLETE) {
return router.push("/(auth)/sign/register");
}
} finally {
onAuthModalClose();
}
};
return (
<View>
<Modal animationType="slide" transparent={true} visible={isVisible}>
<View>
<View>
<Text style={styles.title}>ログインが必要です。</Text>
</View>
<View>
<Pressable onPress={handleLoginButtonPress}>
<View>
<Text>ログインする</Text>
</View>
</Pressable>
<Pressable
onPress={() => {
onAuthModalClose();
}}
>
<View>
<Text>後でする</Text>
</View>
</Pressable>
</View>
<View></View>
</View>
</Modal>
</View>
);
}
1)現在のパスを保存
・ログイン後、ログインページに遷移される前のページに戻るため、現在ページルートを useRouteStoreにあるRedirect情報系のグローバル状態に保存する。
const pathname = usePathname();
setRedirectTo(pathname);2)ログインボタン押下時のロジック
・ログインボタンを押した時、ユーザーの状態を確認し、新規登録ページに遷移させるか、会員情報登録ページに遷移させるか決める。
・finallyを使ってボタン押下後には必ずモーダルの表示情報がCloseになるようにする。
try{
if (!id) {
return router.push("/(auth)/sign/phone");
}
if (status === USER_STATUS_INCOMPLETE) {
return router.push("/(auth)/sign/register");
}
} finally {
onAuthModalClose();
}3)Modalコンポーネント
・animationType:モーダルが表示される時のアニメーション効果。(slideの場合、下から上に上がる。)
・transparent:モーダルの背景を透明にするかのオプション
・visible:モーダルの表示要否オプション
<Modal animationType="slide" transparent={true} visible={isVisible}>
<View>
<View>
<Text style={styles.title}>ログインが必要です。</Text>
</View>
<View>
<Pressable onPress={handleLoginButtonPress}>
<View>
<Text>ログインする</Text>
</View>
</Pressable>
<Pressable
onPress={() => {
onAuthModalClose();
}}
>
<View>
<Text>後でする</Text>
</View>
</Pressable>
</View>
<View></View>
</View>
</Modal>4)使用サンプル
・記事を作成ボタン(赤枠)押下時、ユーザ情報を確認し、条件が満たさない場合、モーダル表示させる。

export default function CommunityListPage() {
const { id, status } = useUserStore();
const { isAuthModalOpen, onAuthModalOpen } = useAuthModalStore();
const handleWriteButtonPress = async () => {
try {
if (isProcessing) return;
setIsProcessing(true);
if (!id || status === USER_STATUS_INCOMPLETE) {
onAuthModalOpen();
return;
}
return router.push("/(modals)/community/write");
} finally {
setIsProcessing(false);
}
};
return (
<View>
<View style={{ zIndex: 10 }}>
...
</View>
<View>{isAuthModalOpen && <RouteAuthModal isVisible={true} />}</View>
<TouchableOpacity
onPress={handleWriteButtonPress}
activeOpacity={0.7}
>
<Text style={styles.buttonText}>
<Ionicons name="add" size={30} color="white" />
</Text>
</TouchableOpacity>
</View>
);
}
)まとめ
このように実装することで、ユーザーが元々行おうとしていた操作の流れを中断せず、ログイン後すぐに続きから利用できるようになります。
また、Zustandを利用してモーダル状態やリダイレクト先をグローバルに管理することで、様々なページから再利用が可能になります。
改善点
モーダルを表示させる条件を各ページではなく、カスタムフックを作ってそこでしたら?各ページはカスタムフックを使うだけで、コードの重複やメンテコストを減らすことができる。