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

**Feature:** <Foldable /> component to React hook #916

Merged
merged 5 commits into from
Feb 13, 2019

Conversation

adeelibr
Copy link
Contributor

@adeelibr adeelibr commented Feb 9, 2019

Summary

Convert Foldable from React class component to React hook.

Related issue

#910

To be tested

Me

  • No error or warning in the console on localhost:6060

@TejasQ TejasQ self-requested a review February 11, 2019 12:36
@TejasQ TejasQ self-assigned this Feb 11, 2019
Copy link
Contributor

@TejasQ TejasQ left a comment

Choose a reason for hiding this comment

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

Beautiful, but I'd request one change.

src/Foldable/Foldable.tsx Outdated Show resolved Hide resolved
src/Foldable/Foldable.tsx Outdated Show resolved Hide resolved
@adeelibr
Copy link
Contributor Author

@TejasQ can you kindly review again.

@TejasQ
Copy link
Contributor

TejasQ commented Feb 12, 2019

Unfortunately, the hover state is quite broken on mouse out.

brokrn

@adeelibr
Copy link
Contributor Author

I am sorry I don't understand, The mouse state seems to be toggling fine at my end via the deployment link for this PR. What is that I am missing here? 🤔

po9jckqueq

I find this as the same behavior as on the https://operational-ui.netlify.com/#Foldable demo as well, please correct me where I am wrong 😅 @TejasQ

@TejasQ
Copy link
Contributor

TejasQ commented Feb 12, 2019

I am sorry I don't understand

No problem at all!

The mouse state seems to be toggling fine at my end

I'm not sure this is true. I think you may not be testing it correctly.

What is that I am missing here? 🤔

Let's clarify with a GIF showing the current master and your PR:

br

Notice how on master, a "flick" of a draggable header works as expected: the hover state clears. In your PR's demo, moving a cursor away from a draggable header quickly causes the mouseout or mouseleave event to be ignored and thus, the header remains grey even after the cursor has exited the draggable header.

Are you able to reproduce this?

@adeelibr
Copy link
Contributor Author

Are you able to reproduce this?

Yes thank you, I am on it.

@adeelibr
Copy link
Contributor Author

Can you kindly review again. @TejasQ 🤞

@TejasQ
Copy link
Contributor

TejasQ commented Feb 12, 2019

@adeelibr great work! 🎉

Let's get one last review from @fabien0102.

Can you explain how you fixed it?

@adeelibr
Copy link
Contributor Author

Can you explain how you fixed it?

+ React.useEffect(() => {
+   document.addEventListener("mousemove", handleMouseMove)
+   return () => document.removeEventListener("mousemove", handleMouseMove)
- }, [])
+ })

Removing [] ensures that the event binding does not happen only on componentDidMount & componentWillUnmount life cycle hook, which was previosly causing glitch & inconsistencies.

The current implementation ensures that as soon as we mouse move over the div element the event as binded & as soon as we mouse out the event is removed. Which ensures that handleMouseMove function is called every time + in doing so ensures that an event is immediately discarded when not in use i.e, 'mousemove' in our case here.

public componentWillUnmount() {
document.removeEventListener("mousemove", this.handleMouseMove)
}
React.useEffect(() => {
Copy link
Contributor

Choose a reason for hiding this comment

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

use effect will run after each render unless you instruct it not to do so with the second param

Copy link
Contributor

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.

Having a look at Dan Abramov's at React Conf 2018 https://youtu.be/V-QO-KO90iQ?t=2475 He isn't monitoring that with a second param. For event listeners.

use effect will run after each render unless you instruct it not to do so with the second param

That is okay! I believe, I have tested this. As soon as I mouse over a the "mousemove" event is binded & as soon as I mouse out the "mousemove" is removed from the DOM for that element. & I think this is the correct behavior. We shouldn't listen to an event continuously which might not get used.

@stereobooster @TejasQ

@TejasQ TejasQ merged commit 5e9b77d into contiamo:master Feb 13, 2019
@TejasQ
Copy link
Contributor

TejasQ commented Feb 13, 2019

@adeelibr let's do a fix in a separate PR.

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.

3 participants