-
-
Save trujic1000/77f075c717cfc01bb01808f09b43f3a3 to your computer and use it in GitHub Desktop.
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
// HOC using Class Component | |
function Wrap(WrappedComponent) { | |
return class extends React.Component { | |
render() { | |
// Copy the props so we don't mutate the original component | |
const newProps = {...this.props}; | |
// Loop through the props and check if the props ia a number | |
for (let [key, value] of Object.entries(newProps)) { | |
if (typeof value === "number") { | |
// Increase the prop | |
newProps[key]++; | |
} | |
} | |
return <WrappedComponent {...newProps} />; | |
} | |
}; | |
} | |
// HOC Using Functional Component | |
const Wrap = WrappedComponent => props => { | |
const newProps = {...props}; | |
for (let [key, value] of Object.entries(newProps)) { | |
if (typeof value === "number") { | |
// Increase the prop | |
newProps[key]++; | |
} | |
} | |
return ( | |
<WrappedComponent {...newProps} /> | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment