Created
March 28, 2021 03:11
-
-
Save TRahulSam1997/d2039242dd28ab220de481c07b9b4afb 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 {createContext, useState} from 'react'; | |
const DataItemsContext = createContext(); | |
const DataItemsProvider = ({children}) => { | |
const [dataItems, setDataItems] = useState([]); | |
const refreshDataItems = async () => { | |
try { | |
const res = await fetch('/api/getData'); | |
const latestDataItems = await res.json(); | |
setDataItems(latestDataItems); | |
} catch(e) { | |
console.error(e) | |
} | |
} | |
return ( | |
<DataItemsContext.Provider | |
value={{ | |
dataItems, | |
setDataItems, | |
refreshDataItems | |
}} | |
> | |
{children} | |
</DataItemsContext.Provider> | |
); | |
} | |
export {DataItemsProvider, DataItemsContext} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment