Last active
October 15, 2019 03:25
-
-
Save pengx17/41a222d1a61acaeae42bba867d2b0d88 to your computer and use it in GitHub Desktop.
Wrap a hook and return a component, see https://stackblitz.com/edit/ripook-demo?file=index.tsx
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 React, { useMemo, useEffect } from 'react'; | |
type Func<Args extends any[], Return> = (...args: Args) => Return; | |
/** | |
* Wrap a hook and return a component | |
*/ | |
export function ripook<Args extends any[], Return>(hook: Func<Args, Return>) { | |
const RipookWrapper: React.FC<{ | |
params: Args; | |
children?: (r: Return) => void; | |
}> = ({ children, params }) => { | |
const res = hook(...params); | |
useEffect(() => { | |
if (typeof children === 'function') { | |
children(res); | |
} | |
}, [res]); // eslint-disable-line | |
return <noscript />; | |
}; | |
return RipookWrapper; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment