Skip to content

Instantly share code, notes, and snippets.

@parvezmrobin
Last active March 24, 2020 13:42
Show Gist options
  • Save parvezmrobin/4cfdb63c2a094bebfba1e13ec0539a59 to your computer and use it in GitHub Desktop.
Save parvezmrobin/4cfdb63c2a094bebfba1e13ec0539a59 to your computer and use it in GitHub Desktop.
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