Skip to main content

Posts

Showing posts with the label hoc

check permission for admin on navbar instead of withSession(App) - on every component

AdminNavbarLinks.js: if ( profile && profile . admin !== true && window . location . href . indexOf ( '/admin' ) !== - 1 ) { router . push ( '/login' ) } const withSession = Component => props => ( < Query query = { GET_ME } > { ({ data , loading , refetch }) => ( < Component { ... props } loading = { loading } session = { data } refetch = { refetch } /> ) } </ Query > ) export default withSession

HOC with hooks - testing hooks

https://github.com/testing-library/react-hooks-testing-library export const Foo = ({ bar }) => { const { result, loading, error } = useRequest(bar); if (error) { return <ErrorComponent />; } else if (loading) { return <LoadingComponent />; } return <ResultComponent result={result} />; }; This looks reasonably good at a glance, but what about when we have to repeat this pattern for a dozen different requests? You would copy and paste this conditional block to each of those different implementations and in each of them, it would need to be tested on its own. An HoC implementation could resemble something like this: const Foo = ({ result }) => <ResultComponent result={result} />; const withRequest = BaseComponent => ({ bar, ...props }) => { const { result, loading, error } = useRequest(bar); return ( <BaseComponent {...props} result={result} loading={loading} error={error} /> ); }; const withError = bra...