import React , { createContext , useMemo , useState } from 'react' ; interface SharedData { totalPageViews : number ; } interface Index { sharedData : SharedData ; setSharedData : ( data : SharedData ) => void ; } const defaultContext = { sharedData : { totalPageViews : 0 } , setSharedData : ( d ) => { throw new Error ( 'setSharedData was not initialized, set it when you invoke the context' ) ; } , } ; export const CsmSharedContext = createContext ( defaultContext ) ; export function CsmSharedContextProvider ( { children , } ) { const [ newData , setNewData ] = useState < SharedData > ( { totalPageViews : 0 } ) ; const setSharedData = React . useCallback ( ( data ) => { setNewData ( data ) ; } , [ ] ) ; const value = useMemo ( ( ) => { return { sharedData : newData , setSharedData } ; } , [ newData , setSharedData ] ) ; return < CsmSharedContext . Provider value = { value } children = { children } / > ; }