From 4e8880881f9353c9b0ca9807e89397b77b48445d Mon Sep 17 00:00:00 2001 From: Ryan Bell <25379378+iRyanBell@users.noreply.github.com> Date: Mon, 19 Oct 2020 21:14:32 -0700 Subject: [PATCH] Update README.md Adds newer functional React syntax --- README.md | 122 +++++++++++++++++++++++++----------------------------- 1 file changed, 56 insertions(+), 66 deletions(-) diff --git a/README.md b/README.md index 67177ed7..b7e3a7a6 100644 --- a/README.md +++ b/README.md @@ -67,21 +67,21 @@ const uiConfig = { // We will display Google and Facebook as auth providers. signInOptions: [ firebase.auth.GoogleAuthProvider.PROVIDER_ID, - firebase.auth.FacebookAuthProvider.PROVIDER_ID - ] + firebase.auth.FacebookAuthProvider.PROVIDER_ID, + ], }; -class SignInScreen extends React.Component { - render() { - return ( -
-

My App

-

Please sign-in:

- -
- ); - } +function SignInScreen() { + return ( +
+

My App

+

Please sign-in:

+ +
+ ); } + +export default SignInScreen ``` ### Using `FirebaseAuth` with local state. @@ -90,7 +90,7 @@ Below is an example on how to use `FirebaseAuth` with a state change upon sign-i ```js // Import FirebaseAuth and firebase. -import React from 'react'; +import React, { useEffect, useState } from 'react'; import StyledFirebaseAuth from 'react-firebaseui/StyledFirebaseAuth'; import firebase from 'firebase'; @@ -102,59 +102,51 @@ const config = { }; firebase.initializeApp(config); -class SignInScreen extends React.Component { - - // The component's Local state. - state = { - isSignedIn: false // Local signed-in state. - }; - - // Configure FirebaseUI. - uiConfig = { - // Popup signin flow rather than redirect flow. - signInFlow: 'popup', - // We will display Google and Facebook as auth providers. - signInOptions: [ - firebase.auth.GoogleAuthProvider.PROVIDER_ID, - firebase.auth.FacebookAuthProvider.PROVIDER_ID - ], - callbacks: { - // Avoid redirects after sign-in. - signInSuccessWithAuthResult: () => false - } - }; +// Configure FirebaseUI. +const uiConfig = { + // Popup signin flow rather than redirect flow. + signInFlow: 'popup', + // We will display Google and Facebook as auth providers. + signInOptions: [ + firebase.auth.GoogleAuthProvider.PROVIDER_ID, + firebase.auth.FacebookAuthProvider.PROVIDER_ID + ], + callbacks: { + // Avoid redirects after sign-in. + signInSuccessWithAuthResult: () => false, + }, +}; + +function SignInScreen() { + const [isSignedIn, setIsSignedIn] = useState(false); // Local signed-in state. // Listen to the Firebase Auth state and set the local state. - componentDidMount() { - this.unregisterAuthObserver = firebase.auth().onAuthStateChanged( - (user) => this.setState({isSignedIn: !!user}) - ); - } - - // Make sure we un-register Firebase observers when the component unmounts. - componentWillUnmount() { - this.unregisterAuthObserver(); - } - - render() { - if (!this.state.isSignedIn) { - return ( -
-

My App

-

Please sign-in:

- -
- ); - } + useEffect(() => { + const unregisterAuthObserver = firebase.auth().onAuthStateChanged(user => { + setIsSignedIn(!!user); + }); + return () => unregisterAuthObserver(); // Make sure we un-register Firebase observers when the component unmounts. + }, []); + + if (!isSignedIn) { return (

My App

-

Welcome {firebase.auth().currentUser.displayName}! You are now signed-in!

- firebase.auth().signOut()}>Sign-out +

Please sign-in:

+
); } + return ( +
+

My App

+

Welcome {firebase.auth().currentUser.displayName}! You are now signed-in!

+ firebase.auth().signOut()}>Sign-out +
+ ); } + +export default SignInScreen; ``` ### Accessing the FirebaseUI instance @@ -165,15 +157,13 @@ To do this you can pass a `uiCallback` callback function that wil be passed the ```js // ... -render() { - return ( -
-

My App

-

Please sign-in:

- ui.disableAutoSignIn()} uiConfig={this.uiConfig} firebaseAuth={firebase.auth()}/> -
- ); -} +return ( +
+

My App

+

Please sign-in:

+ ui.disableAutoSignIn()} uiConfig={uiConfig} firebaseAuth={firebase.auth()}/> +
+); ```