Skip to content
Permalink
Browse files

Created react-use-firebase

  • Loading branch information
jln-dk committed Feb 7, 2020
0 parents commit 2772ea94710f78737d6cb6a1823e90ffb1ee40a7
Showing with 7,173 additions and 0 deletions.
  1. +5 −0 .gitignore
  2. +21 −0 LICENSE
  3. +59 −0 package.json
  4. +52 −0 src/FirebaseProvider.tsx
  5. +6 −0 src/FirebaseProviderContext.ts
  6. +4 −0 src/index.ts
  7. +25 −0 src/types.ts
  8. +8 −0 src/useFirebase.ts
  9. +30 −0 tsconfig.json
  10. +6,963 −0 yarn.lock
@@ -0,0 +1,5 @@
*.log
.DS_Store
node_modules
.cache
dist
21 LICENSE
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2020 jln-dk

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
@@ -0,0 +1,59 @@
{
"name": "react-use-firebase",
"version": "0.1.0",
"description": "React hook for easy integration with Firebase",
"keywords": [
"react",
"hook",
"firebase",
"use-firebase"
],
"author": "Jesper Lund (https://github.com/jln-dk)",
"repository": {
"type": "git",
"url": "https://github.com/jln-dk/react-use-firebase"
},
"homepage": "https://github.com/jln-dk/react-use-firebase#readme",
"license": "MIT",
"module": "dist/react-use-firebase.esm.js",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
"files": [
"dist"
],
"scripts": {
"start": "tsdx watch",
"build": "tsdx build",
"test": "tsdx test --passWithNoTests",
"lint": "tsdx lint",
"prepare": "tsdx build"
},
"dependencies": {
"firebase": "^7.8.1"
},
"devDependencies": {
"@types/jest": "^25.1.2",
"@types/react": "^16.9.19",
"@types/react-dom": "^16.9.5",
"husky": "^4.2.1",
"react": "^16.12.0",
"react-dom": "^16.12.0",
"tsdx": "^0.12.3",
"tslib": "^1.10.0",
"typescript": "^3.7.5"
},
"peerDependencies": {
"react": ">=16"
},
"husky": {
"hooks": {
"pre-commit": "tsdx lint"
}
},
"prettier": {
"printWidth": 80,
"semi": true,
"singleQuote": true,
"trailingComma": "es5"
}
}
@@ -0,0 +1,52 @@
import React, { useEffect, useState } from 'react';
import { FirebaseProviderContext } from './FirebaseProviderContext';
import { FirebaseProviderProps } from './types';
import * as firebase from 'firebase/app';
import 'firebase/auth';

export const FirebaseProvider = (props: FirebaseProviderProps) => {
const [loaded, setLoaded] = useState(false);
const [
firebaseInstance,
setFirebaseInstance,
] = useState<firebase.app.App | null>(null);
const [firebaseUser, setFirebaseUser] = useState<firebase.User | null>(null);

useEffect(() => {
// Initialize Firebase app on mount and config changes
const app = firebase.initializeApp(props.config);
setFirebaseInstance(app);
}, [props.config]);

useEffect(() => {
// Check if Firebase instance is valid
if (!firebaseInstance) {
return;
}

// Subscribe to user auth changes
const unsubscribe = firebase.auth().onAuthStateChanged(user => {
if (!loaded) {
setLoaded(true);
}
setFirebaseUser(user);
});

// Cleanup subscription on unmount
return () => unsubscribe();
}, [firebaseInstance, loaded]);

// Check if provider has loaded and Firebase instance is valid
if (!loaded || !firebaseInstance) {
// Show either fallback or null
return props.fallback || null;
}

return (
<FirebaseProviderContext.Provider
value={{ firebase: firebaseInstance, user: firebaseUser }}
>
{props.children}
</FirebaseProviderContext.Provider>
);
};
@@ -0,0 +1,6 @@
import { createContext } from 'react';
import { FirebaseProviderContextType } from './types';

export const FirebaseProviderContext = createContext<
FirebaseProviderContextType
>({} as FirebaseProviderContextType);
@@ -0,0 +1,4 @@
export * from './FirebaseProvider';
export * from './FirebaseProviderContext';
export * from './useFirebase';
export * from './types';
@@ -0,0 +1,25 @@
import * as firebase from 'firebase';

export type FirebaseConfig = {
apiKey: string;
authDomain: string;
databaseURL: string;
projectId: string;
storageBucket: string;
messagingSenderId: string;
appId: string;
measurementId: string;
};

export type FirebaseProviderContextType = {
firebase: firebase.app.App;
user: firebase.User | null;
};

export type FirebaseProviderProps = {
config: FirebaseConfig;
children?: JSX.Element;
fallback?: JSX.Element;
};

export type FirebaseHookReturnType = [firebase.app.App, firebase.User | null];
@@ -0,0 +1,8 @@
import { useContext } from 'react';
import { FirebaseProviderContext } from './FirebaseProviderContext';
import { FirebaseHookReturnType } from './types';

export const useFirebase = (): FirebaseHookReturnType => {
const { firebase, user } = useContext(FirebaseProviderContext);
return [firebase, user];
};
@@ -0,0 +1,30 @@
{
"include": ["src", "types", "test"],
"compilerOptions": {
"target": "es5",
"module": "esnext",
"lib": ["dom", "esnext"],
"importHelpers": true,
"declaration": true,
"sourceMap": true,
"rootDir": "./",
"strict": true,
"noImplicitAny": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"strictPropertyInitialization": true,
"noImplicitThis": true,
"alwaysStrict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"moduleResolution": "node",
"baseUrl": "./",
"paths": {
"*": ["src/*", "node_modules/*"]
},
"jsx": "react",
"esModuleInterop": true
}
}

0 comments on commit 2772ea9

Please sign in to comment.
You can’t perform that action at this time.