1.0.1 • Published 3 years ago
@cprecioso/react-hooks v1.0.1
React Hook utils
$ npm i -D @cprecioso/react-hooks # if you use npm
$ yarn add --dev @cprecioso/react-hooks # if you use yarnuseStrictMemo
import { useStrictMemo } from "@cprecioso/react-hooks";
const SpotifyPlayer = ({ loginToken }) => {
const makePlayer = useCallback(
() => new Spotify.Player({ loginToken }),
[loginToken]
);
const player = useStrictMemo(makePlayer);
return <button onClick={() => player.start()}>Play</button>;
};A copy of useMemo, but guaranteeing that the passed function is only re-run
when it changes.
It expects your function to be wrapped in a useCallback with a dependency
array. Don't pass the dependency array directly here.
React's
useMemocan throw away the result and re-run the function at any time, even if the runtime doesn't currently do it.