Last active
March 24, 2020 13:42
-
-
Save parvezmrobin/4cfdb63c2a094bebfba1e13ec0539a59 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, {createContext, useState} from 'react'; | |
import firebase from '../firebase'; | |
export const ChatContext = createContext(); | |
const ChatContextProvider = (props) => { | |
//Firebase settings | |
const DB = firebase.firestore(); | |
const chatRef = DB.collection("chats"); | |
//States | |
const [chatList, setChatList] = useState([]); | |
//console.log(chatList); | |
//Method to fetch chat list from Firebase | |
const getChatList= () => { | |
if('chats' in localStorage) { | |
const chats = JSON.parse(localStorage.getItem('chats')); | |
setChatList([chats]); | |
} | |
chatRef | |
.doc('JyNeS95OnZ3qLDno3EVd') | |
.onSnapshot((querySnapshot) => { | |
setChatList([querySnapshot.data().c]); | |
localStorage.setItem('chats', JSON.stringify(querySnapshot.data().c)); | |
}); | |
} | |
return ( | |
<ChatContext.Provider value={{chatList, getChatList}}> | |
{props.children} | |
</ChatContext.Provider> | |
); | |
} | |
export default ChatContextProvider; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment