Skip to content

Instantly share code, notes, and snippets.

@brunormferreira
Created December 29, 2020 13:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brunormferreira/e1e876807782e6a0ae4d2cfe88fa8401 to your computer and use it in GitHub Desktop.
Save brunormferreira/e1e876807782e6a0ae4d2cfe88fa8401 to your computer and use it in GitHub Desktop.
import React from 'react';
function Counter(props) {
const { counter, setCounter } = props;
return (
<div>
<h1>Functional Component Example for Counter</h1>
<p>We clicked {counter} times</p>
<button onClick={() => setCounter(counter + 1)}>Increase counter</button>
</div>
);
}
export default withCounterState(Counter);
function withCounterState(WrappedComponent) {
return function (...props) {
const [counter, setCounter] = React.useState(0);
props['counter'] = counter;
props['setCounter'] = setCounter;
return <WrappedComponent {...props} />;
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment