Last active
December 2, 2019 17:33
-
-
Save react-ram/aefca133e3c36ecacec446ce7946c2b9 to your computer and use it in GitHub Desktop.
Higher Order Component example
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
import React, { Component } from "react"; | |
import ClickCounter from "./ClickCounter"; | |
import HoverCounter from "./HoverCounter"; | |
class App extends Component { | |
render() { | |
return ( | |
<div> | |
<ClickCounter /> | |
<HoverCounter /> | |
</div> | |
); | |
} | |
} | |
export default App; |
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
import React, { Component } from "react"; | |
import withCounter from "./withCounter"; | |
class ClickCounter extends Component { | |
render() { | |
const { count, incrementCount } = this.props; | |
return ( | |
<div> | |
<button onClick={incrementCount}>clicked {count} times</button> | |
</div> | |
); | |
} | |
} | |
export default withCounter(ClickCounter); |
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
import React, { Component } from "react"; | |
import withCounter from "./withCounter"; | |
export class HoverCounter extends Component { | |
render() { | |
const { count, incrementCount } = this.props; | |
return ( | |
<div> | |
<h2 onMouseOver={incrementCount}> hovered {count} times</h2> | |
</div> | |
); | |
} | |
} | |
export default withCounter(HoverCounter); |
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
import React from "react"; | |
const withCounter = WrappedComponent => { | |
class WithCounter extends React.Component { | |
constructor(props) { | |
super(props); | |
this.state = { | |
count: 0 | |
}; | |
} | |
incrementCount = () => { | |
this.setState(prevState => ({ count: prevState.count + 1 })); | |
}; | |
render() { | |
return ( | |
<WrappedComponent | |
count={this.state.count} | |
incrementCount={this.incrementCount} | |
/> | |
); | |
} | |
} | |
return WithCounter; | |
}; | |
export default withCounter; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A higher-order component (HOC) is an advanced technique in React for reusing component logic.Concretely, a higher-order component is a function that takes a component and returns a new component.