Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ce50e03
commit 64355d5
Showing
2 changed files
with
51 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import React from 'react' | ||
|
||
export default function withHover (Component, propName = 'hovering') { | ||
return class WithHover extends React.Component { | ||
constructor(props) { | ||
super(props) | ||
|
||
this.state = { | ||
hovering: false, | ||
} | ||
|
||
this.mouseOver = this.mouseOver.bind(this) | ||
this.mouseOut = this.mouseOut.bind(this) | ||
} | ||
mouseOver() { | ||
this.setState({ | ||
hovering: true | ||
}) | ||
} | ||
mouseOut() { | ||
this.setState({ | ||
hovering: false | ||
}) | ||
} | ||
render () { | ||
const props = { | ||
[propName]: this.state.hovering, | ||
...this.props | ||
} | ||
|
||
return ( | ||
<div onMouseOver={this.mouseOver} onMouseOut={this.mouseOut}> | ||
<Component {...props} /> | ||
</div> | ||
) | ||
} | ||
} | ||
} |
64355d5
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It may be obvious to some but I found I could not change the name of hovering in ToolTip, for example to "hover" in lines 27 and 30 above without also changing the propName in line 3 of withHover to "hover". Leaving line 3's propName to "hovering" resulted in an error undefined.
Warning: Failed prop type: The prop
hoveringis marked as required in
ToolTip, but its value is
undefined.
64355d5
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I understood how Higher order functions work, Here it is passed a function like this which is later rendered -->
export default withHover(Tooltip)
My question is can we do something so that we can render like this by exporting it.
64355d5
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I achieved it by doing this inside the Popular component-->and i exported the Tootltip function as well.
64355d5
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think propName='hover' is a default parameter and shoudn't need to be changed. If you pass in 'hover' as a parameter to withHover(Tooltip, 'hover'), you wont get that error message.