Skip to content

Instantly share code, notes, and snippets.

@GeoffMahugu
Created May 15, 2021 19:39
Show Gist options
  • Select an option

  • Save GeoffMahugu/8f24dd45431a6079a035fb54d2ce6d4e to your computer and use it in GitHub Desktop.

Select an option

Save GeoffMahugu/8f24dd45431a6079a035fb54d2ce6d4e to your computer and use it in GitHub Desktop.
This is the App.tsx file for the react-firebase app
import React, { useEffect, useState } from 'react';
import { BrowserRouter as Router, Switch, Route, RouteComponentProps } from 'react-router-dom';
import { auth } from './config/firebase';
import routes from './config/routes';
import AuthRoute from './modules/auth/AuthRouter';
import './App.css';
export interface IApplicationProps { }
const App: React.FunctionComponent<IApplicationProps> = props => {
const [loading, setLoading] = useState<boolean>(true);
// Monitor and Update user state.
useEffect(() => {
auth.onAuthStateChanged(user => {
if (user) {
console.log('User detected.')
} else {
console.log('No user detected');
}
setLoading(false);
})
}, []);
if (loading)
return <div>Loding...</div>
return (
<Router>
<Switch>
{routes.map((route, index) =>
<Route
key={index}
path={route.path}
exact={route.exact}
render={(routeProps: RouteComponentProps<any>) => {
if (route.protected)
return <AuthRoute ><route.component {...routeProps} /></AuthRoute>;
return <route.component {...routeProps} />;
}}
/>)}
</Switch>
</Router>
);
}
export default App;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment