-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Higher Order Component
- Loading branch information
Showing
5 changed files
with
96 additions
and
2 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
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,17 @@ | ||
import React, { Component } from "react"; | ||
import withCounter from "./withCounter"; | ||
|
||
class ClickCounter extends Component { | ||
render() { | ||
const { count, incrementCount, name } = this.props; | ||
return ( | ||
<div> | ||
<button onClick={incrementCount}> | ||
{name} Clicked {count} times | ||
</button> | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
export default withCounter(ClickCounter, 5); |
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,17 @@ | ||
import React, { Component } from "react"; | ||
import withCounter from "./withCounter"; | ||
|
||
class HoverCounter extends Component { | ||
render() { | ||
const { count, incrementCount, name } = this.props; | ||
return ( | ||
<div> | ||
<h1 onMouseOver={incrementCount}> | ||
{name} Hover {count} times | ||
</h1> | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
export default withCounter(HoverCounter, 10); |
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,37 @@ | ||
import React from "react"; | ||
|
||
const withCounter = (WrappedComponent, incrementNumber) => { | ||
class WithCounter extends React.Component { | ||
constructor(props) { | ||
super(props); | ||
|
||
this.state = { | ||
count: 0, | ||
}; | ||
} | ||
|
||
incrementCount = () => { | ||
this.setState((prevState) => { | ||
return { count: prevState.count + incrementNumber }; | ||
}); | ||
}; | ||
|
||
render() { | ||
return ( | ||
<WrappedComponent | ||
count={this.state.count} | ||
incrementCount={this.incrementCount} | ||
{...this.props} | ||
/> | ||
); | ||
} | ||
} | ||
return WithCounter; | ||
}; | ||
|
||
export default withCounter; | ||
|
||
/* | ||
To share common functionality between components. | ||
A Higher Order Components (HOC) pattern where a function takes a component as an argument and returns a new component. | ||
*/ |