Created
December 29, 2020 13:52
-
-
Save brunormferreira/e1e876807782e6a0ae4d2cfe88fa8401 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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