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

Rewrite <HoverLayer> using Hooks #10

Merged
merged 5 commits into from
Mar 4, 2019

Conversation

hsunpei
Copy link
Contributor

@hsunpei hsunpei commented Feb 26, 2019

Purpose

This PR follows the rewrites using Hooks (#8, #9). It converts as a functional component.

In addition, to better reuses the process of requesting animation frame, it adds a new custom effect useAnimationFrame of which users can simply call its requestWindowAnimationFrame, and it will cancel the animation frame when the component is going to be unmounted.

Changes

  • Rewrite HoverLayer using Hooks
  • Extract the animation frame controls as a custom effect hook

@hsunpei hsunpei self-assigned this Feb 26, 2019
return (
<>
{detectionAreas}
</>
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe we don't need the Fragment, just return detectionAreas as previous code?

Copy link
Contributor Author

@hsunpei hsunpei Mar 4, 2019

Choose a reason for hiding this comment

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

This is necessary for Typescript because if detectionAreas were not wrapped by Fragment, <HoverLayer> would not be a legit FunctionComponent.

Copy link
Contributor

Choose a reason for hiding this comment

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

Interesting 🤕 I just tried directly return detectionAreas and the typescript complier / tslint didn't have any error / warning for this.

Copy link
Contributor

@chenesan chenesan left a comment

Choose a reason for hiding this comment

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

Looks great! Needs to tweak some issues around useCallback, though.

},
throttleTime,
),
[],
Copy link
Contributor

Choose a reason for hiding this comment

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

I think the second args should be [handleHover, setHoveredPosAndIndex, throttleTime]. The throttled function should change if one of these props changes. See the note part of useCallback doc.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Great catch. Thanks for finding this out 😃

event.persist();
updatePosition(dataIndex, event);
},
[],
Copy link
Contributor

Choose a reason for hiding this comment

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

Same as the comment above (L63).

updatePosition.cancel();
clearHovering();
},
[],
Copy link
Contributor

Choose a reason for hiding this comment

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

Same as the comment above with one more clearHovering argument in input arrays.

@hsunpei
Copy link
Contributor Author

hsunpei commented Mar 4, 2019

@chenesan Thanks for reminding me about the correct usage of useCallback. You may take a look at the PR when you're free 😃

Copy link
Contributor

@chenesan chenesan left a comment

Choose a reason for hiding this comment

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

Looks good to me now 👍

@hsunpei hsunpei merged commit 42cb371 into feature/responsive-hook Mar 4, 2019
@chenesan chenesan mentioned this pull request Mar 18, 2019
@zhusee2 zhusee2 deleted the feature/hoverlayer-hook branch September 9, 2019 10:22
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.

2 participants