Skip to content
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

returned functions of useModal hook have "unstable" identity #53

Closed
janczizikow opened this issue Apr 6, 2022 · 1 comment
Closed

Comments

@janczizikow
Copy link
Contributor

First of thanks a lot for a cool library 😁

I found that function identity of "handlers" returned from useModal hook isn't stable. It changes on some renders making it a bit hard to reliably use with other hooks. For example:

const [counter, setCounter] = React.useState(0);
const userModal = useModal(UserInfoModal);
const { show } = userModal;

React.useEffect(() => {
  if (counter >= 2) {
    show();
  }
}, [counter, show]) // <- once `counter` reaches 2 the component will continue to infinitely re-render

I think it would be a nice improvement to memoize the handlers in a way that they wouldn't change depending on the state - right now calling show() will change the identity of show 🤔
A small demo showcasing the issue: https://codesandbox.io/s/nice-modal-lqj0kg?file=/src/UserList.tsx

@janczizikow
Copy link
Contributor Author

I was investigating this further and created a PR for this. The fix seems pretty simple 🙂

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

No branches or pull requests

1 participant