purescript-react / purescript-react-basic-hooks

An implementation of React hooks on top of purescript-react-basic
https://pursuit.purescript.org/packages/purescript-react-basic-hooks/
Apache License 2.0
198 stars 31 forks source link

Use JS implementation of `useEffectAlways` #26

Closed pete-murphy closed 4 years ago

pete-murphy commented 4 years ago

The existing version of useEffectAlways is passing unit as a dependency. The memoizedKey here is therefore always the same, referentially-equal value (despite eq constantly returning false).

exports.useEffect_ = function (eq, deps, effect) {
  var memoizedKey = exports.useMemo_(eq, deps);
  React.useEffect(effect, [memoizedKey]);
};

Because the memoizedKey never changes, this effect will only run once, not on every render (as I think was intended). This behavior can be witnessed in this example: https://codesandbox.io/s/use-effect-unit-dep-8ec3l?file=/src/App.js

I don't know if there's a way around this using current API of react-basic-hooks, so the proposed (not ideal 😕 ) solution in this PR is to add a new foreign function useEffectAlways_ that omits the dependency array.

Fixes #25