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

feat: improve useClickAway() hook #394

Merged
merged 2 commits into from
Jun 19, 2019
Merged

feat: improve useClickAway() hook #394

merged 2 commits into from
Jun 19, 2019

Conversation

jeetiss
Copy link
Contributor

@jeetiss jeetiss commented Jun 14, 2019

No description provided.

jeetiss added 2 commits June 14, 2019 22:18
add deps for skip useless event sub/unsub
@@ -8,10 +8,14 @@ const useClickAway = (
onClickAway: (event: KeyboardEvent) => void,
events: string[] = defaultEvents
) => {
const savedCallback = useRef(onClickAway);
Copy link
Owner

Choose a reason for hiding this comment

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

Instead of saving the callback inside the useClickAway hook, isn't it better if consumer do it manually?

const onClickAway = useCallback(() => { /* ... */ });
useClickAway(ref, onClickAway);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't want to write extra code on every useClickAway usage
and this approach is less effective

Choose a reason for hiding this comment

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

may I ask what is the purpos of using the useRef here ?
why not simply use the callback ?

@@ -21,7 +25,7 @@ const useClickAway = (
off(document, eventName, handler);
}
};
});
}, [events, ref]);
Copy link
Owner

Choose a reason for hiding this comment

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

events is an array, I though array cannot be a dep, maybe it should be like this:

[...events, ref]

Copy link
Contributor Author

@jeetiss jeetiss Jun 19, 2019

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sorry, I'm trying to figure it out myself.

And you approach is totally wrong, with spread operator in deps react writes this warnings


Warning: The final argument passed to useEffect changed size between renders. The order and size of this array must remain constant.

Previous: [1, 2, 3, 1, 1, 1]
Incoming: [1, 2, 3]
    in App

Copy link
Owner

Choose a reason for hiding this comment

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

Could useEffect be changed to useDeepCompareEffect?

Copy link
Contributor Author

@jeetiss jeetiss Jun 19, 2019

Choose a reason for hiding this comment

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

Why? useEffect works fine with array!
See codesandbox example

Copy link
Owner

@streamich streamich Jun 19, 2019

Choose a reason for hiding this comment

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

I guess you are right. I was thinking if events is provided inline, it will be a new array every re-render.

useClickAway(ref, fn, ['mousedown', 'touchstart'])

User needs to make sure events is memoized.

Copy link
Owner

@streamich streamich left a comment

Choose a reason for hiding this comment

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

Thanks!

@streamich streamich changed the title improve: click away hook feat: improve useClickAway() hook Jun 19, 2019
@streamich streamich merged commit c60df19 into streamich:master Jun 19, 2019
streamich pushed a commit that referenced this pull request Jun 19, 2019
# [9.7.0](v9.6.0...v9.7.0) (2019-06-19)

### Features

* improve useClickAway() hook ([#394](#394)) ([c60df19](c60df19))
@streamich
Copy link
Owner

🎉 This PR is included in version 9.7.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants