Using hooks with props
Jonhyfun opened this issue · 2 comments
Jonhyfun commented
Does usebetween work properly if i have a hook that recieves props?
Eslinter complains i cant use a hook inside a callback and there are no examples in the documentation.
I tried using it like (props) => useBetween((props) => useLayoutModalsBase(props))
and it doesnt work, i believe it might be because that creates a new hook instance everytime i call it
betula commented
Hello @Jonhyfun!
Thanks!
Exists two different logic in that case:
const { setMyValue } = useMySharedState();
// Put component's prop here
const myValue = props.myValue;
// And update shared state when component’s prop changed
useEffect(() => {
setMyValue(myValue);
}, [myValue]);
and
import memoize from "memoizee";
const useCustomHookMemoized = memoize((prop1, prop2) => {
// Thanks to the memoization tool, we got all the closure parameters we need.
return () =>
// Parameterized shared hook here.
useCustomHook({ prop1, prop2 });
});
// As a result, we need to make a separate shared state for each set of arguments for calling the "useCustomHook" hook.
export const useSharedCustomHook = ({ prop1, prop2 }) => {
return useBetween(useCustomHookMemoized(prop1, prop2));
};
Hope it's convenient for you
Jonhyfun commented
I see, thank you