import { useContext } from 'react' ; import { GetStaticPropsContext , GetServerSidePropsContext } from 'next' ; import NextDataHooksContext from './next-data-hooks-context' ; const stub = ( ) => { throw new Error ( 'Create data hook was run in the browser. See https://github.com/ricokahler/next-data-hooks#code-elimination' ) ; } ; param key param getData return function createDataHook < R > ( key , getData ) { function useData ( ) { const dataHooksContext = useContext ( NextDataHooksContext ) ; if ( ! dataHooksContext ) { throw new Error ( 'Could not find `NextDataHooksContext`. Ensure `NextDataHooksProvider` is configured correctly.' ) ; } const dataHooksValue = dataHooksContext [ key ] ; if ( ! Object . keys ( dataHooksContext ) . includes ( key ) ) { throw new Error ( `Did not find a data hook named "${ key }". Ensure it was provided to getDataHooksProps.` ) ; } return dataHooksValue ; } return Object . assign ( useData , { getData : getData || stub , key , } ) ; } export default createDataHook ;