reactでログイン画面の作成をしています。
react-router-domはv6です。
useNavigateを読み込んで、login後home('/')に遷移させたいのですが、下記エラーが出ます。
React Hook "useNavigate" is called in function "signup" that is neither a React function component nor a custom React Hook function. React component names must start with an uppercase letter. React Hook names must start with the word "use"
AuthProvider.js
1import React, { useEffect, useState } from "react"; 2import {auth} from "../firebase/firebase"; 3import { useNavigate } from 'react-router-dom'; 4 5const AuthContext = React.createContext() 6const AuthProvider = ( { children }) => { 7 const [currentUser, setCurrentUser] =useState(null); 8 9 const signup = async ( email, password ) =>{ 10 try{ 11 const navigate = useNavigate(); 12 await auth.createUserWithEmailAndPassword( email, password ); 13 auth.onAuthStateChanged( user => setCurrentUser(user)); 14 navigate('/'); 15 } 16 catch( error ) { 17 alert( error ); 18 } 19 } 20 21const login = async ( email, password ) =>{ 22 try{ 23 const navigate = useNavigate(); 24 await auth.signInWithEmailAndPassword( email ,password ); 25 auth.onAuthStateChanged( user => setCurrentUser( user )); 26 navigate('/'); 27 } catch( error ){ 28 console.log( error ); 29 } 30} 31useEffect(() =>{ 32 auth.onAuthStateChanged( setCurrentUser ); 33}, []); 34 35return( 36 <AuthContext.Provider value={{ signup, login, currentUser}}> 37 {children} 38 </AuthContext.Provider>) 39} 40export {AuthContext, AuthProvider}
フックを使用したメソッド名はuseから始めているし、頭文字を大文字に変更してもエラーは変わらず、もし解決方法をご存知の方がいらっしゃいましたら、回答いただけますと大変助かります。
回答1件
あなたの回答
tips
プレビュー