betula/use-between

Using hooks with props

Jonhyfun opened this issue · 2 comments

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

Hello @Jonhyfun!
Thanks!

Exists two different logic in that case:

#15 (comment)

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

#13 (comment)

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

I see, thank you