ReactJS - How to implement custom hook with intial-state function
I want to impl. a custom hook, the initial state has to be computed and I do not want to compute the initial state on each re-render.
So I want to pass my custom hook an init-state function, exactly like you can do e.g. with useState(() => ({ ... }))
.
It would be nice if the solution would be in TypeScript.
Demo for my issue:
interface IUseMyCustomHook {
foo: string;
bar: string;
}
const useMyCustomHook = ({ foo, bar }: IUseMyCustomHook) => {
return {
...
};
}
const MyComponent = () => {
const { ... } = useMyCustomHook(() => doSomeHeavyCalc());
....
}
1399 views