Skip to content

Instantly share code, notes, and snippets.

@trujic1000
Created June 21, 2020 15:01
Show Gist options
  • Save trujic1000/77f075c717cfc01bb01808f09b43f3a3 to your computer and use it in GitHub Desktop.
Save trujic1000/77f075c717cfc01bb01808f09b43f3a3 to your computer and use it in GitHub Desktop.
// 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