Skip to content

Commit

Permalink
🚀 Higher Order Component
Browse files Browse the repository at this point in the history
Higher Order Component
  • Loading branch information
SaishJ committed Sep 12, 2022
1 parent 83c11fa commit 1d7a901
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 2 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -219,3 +219,22 @@ export default ClassClick;
### [Forwarding Refs](https://github.com/SaishJ/React-JS-Beginner-to-Advanced/commit/9a57ffbe3d6eeea400c3604bc66fe6bb6bafabdd)

### [Portals](https://github.com/SaishJ/React-JS-Beginner-to-Advanced/commit/89a9384c40963c77ec47ac76cbd8699ee1bebe4d)

### [Error Boundary](https://github.com/SaishJ/React-JS-Beginner-to-Advanced/commit/83c11fa8ef5b3c7cf3a0a71495993ea9fed97053)

### [Higher Order Component]()

```jsx
import React from "react";

const UpdatedComponent = (OriginalComponent) => {
class NewComponent extends React.Component {
render() {
return <OriginalComponent />;
}
}
return NewComponent;
};

export default UpdatedComponent;
```
8 changes: 6 additions & 2 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,23 @@ import FRParentInput from "./components/FRParentInput";
import PortalDemo from "./components/PortalDemo";
import Hero from "./components/Hero";
import ErrorBoundary from "./components/ErrorBoundary";
import ClickCounter from "./components/ClickCounter";
import HoverCounter from "./components/HoverCounter";

function App() {
return (
<div className="App">
<ErrorBoundary>
<ClickCounter name="Saish" />
<HoverCounter name="Pratik" />
{/* <ErrorBoundary>
<Hero heroName="Superman" />
</ErrorBoundary>
<ErrorBoundary>
<Hero heroName="Batman" />
</ErrorBoundary>
<ErrorBoundary>
<Hero heroName="Joker" />
</ErrorBoundary>
</ErrorBoundary> */}
{/* <PortalDemo /> */}
{/* <FRParentInput /> */}
{/* <FocusInput /> */}
Expand Down
17 changes: 17 additions & 0 deletions src/components/ClickCounter.js
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);
17 changes: 17 additions & 0 deletions src/components/HoverCounter.js
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);
37 changes: 37 additions & 0 deletions src/components/withCounter.js
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.
*/

0 comments on commit 1d7a901

Please sign in to comment.