Skip to content

Use JS implementation of useEffectAlways #26

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

pete-murphy
Copy link
Member

@pete-murphy pete-murphy commented Apr 26, 2020

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

Copy link
Member

@maddie927 maddie927 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like the right fix to me, thank you!

@maddie927 maddie927 merged commit 8e5fedd into purescript-react:master Apr 27, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

useEffectAlways does not always run
2 participants