I just try to create a React Native library, and currently I’ve a use case that my function can be declared using
const show = useMyHook()
or
const {show, hide} = useMyHook()
useMyHook.js
const useMyHook = () => {
const MyCTX = useContext(MyContext);
if (!MyCTX) {
throw new Error('useMyHook must be defined.');
}
// how do i conditionally return
// return {show: MyCTX.show, hide:MyCTX.hide}
// and
// return MyCTX.show
return MyCTX.show;
};
export default useMyHook;
what should i do to make useMyHook()
accept both of them? or is it not possible?
the main reason why I want useMyHook()
can accept both of them is, so dev can naming anything into variable when they only want to call show()
as a function like so:
const showMyHookInBannerHomepage = useMyHook()
i think it’s more simple rather than
const {show:showMyHookInBannerHomepage} = useMyHook()
2
Answers
One way which I can think of is to make the hook work for both use cases by returning a function with properties attached to it. Something like:
SAMPLE DEMO
Alternative Approach
Another way would require you to use a parameter(if you like to change the signature of your hook) to your hook based on which you can conditionally use them. A sample would look like:
SIDENOTE:
Note that destructuring assignment will happily ignore any properties that aren’t mentioned; so even without doing anything special, any callers that don’t need
hide
can simply write:That’s what I’d recommend unless you have a particularly strong reason to do something different. I say this not because the above is trivial to implement (though it is), but because it’s trivial to understand. Any other approach makes it hard to describe what
useMyHook
returns.