55 sendEmailVerification ,
66 UserCredential ,
77} from 'firebase/auth' ;
8- import { useMemo , useState } from 'react' ;
8+ import { useCallback , useState } from 'react' ;
99import { CreateUserOptions , EmailAndPasswordActionHook } from './types' ;
1010
1111export default (
@@ -16,37 +16,31 @@ export default (
1616 const [ registeredUser , setRegisteredUser ] = useState < UserCredential > ( ) ;
1717 const [ loading , setLoading ] = useState < boolean > ( false ) ;
1818
19- const createUserWithEmailAndPassword = async (
20- email : string ,
21- password : string
22- ) => {
23- setLoading ( true ) ;
24- setError ( undefined ) ;
25- try {
26- const user = await firebaseCreateUserWithEmailAndPassword (
27- auth ,
28- email ,
29- password
30- ) ;
31- if ( options && options . sendEmailVerification && user . user ) {
32- await sendEmailVerification (
33- user . user ,
34- options . emailVerificationOptions
19+ const createUserWithEmailAndPassword = useCallback (
20+ async ( email : string , password : string ) => {
21+ setLoading ( true ) ;
22+ setError ( undefined ) ;
23+ try {
24+ const user = await firebaseCreateUserWithEmailAndPassword (
25+ auth ,
26+ email ,
27+ password
3528 ) ;
29+ if ( options && options . sendEmailVerification && user . user ) {
30+ await sendEmailVerification (
31+ user . user ,
32+ options . emailVerificationOptions
33+ ) ;
34+ }
35+ setRegisteredUser ( user ) ;
36+ } catch ( error ) {
37+ setError ( error as AuthError ) ;
38+ } finally {
39+ setLoading ( false ) ;
3640 }
37- setRegisteredUser ( user ) ;
38- } catch ( error ) {
39- setError ( error as AuthError ) ;
40- } finally {
41- setLoading ( false ) ;
42- }
43- } ;
41+ } ,
42+ [ auth , options ]
43+ ) ;
4444
45- const resArray : EmailAndPasswordActionHook = [
46- createUserWithEmailAndPassword ,
47- registeredUser ,
48- loading ,
49- error ,
50- ] ;
51- return useMemo < EmailAndPasswordActionHook > ( ( ) => resArray , resArray ) ;
45+ return [ createUserWithEmailAndPassword , registeredUser , loading , error ] ;
5246} ;
0 commit comments