Created
January 10, 2020 22:32
-
-
Save DaddyMoe/b0b8e91765e0d0ad259483c12fde37bd to your computer and use it in GitHub Desktop.
React state management and Components reusing
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
// Use https://jscomplete.com/playground/rgs to play with this | |
function Button(props) { | |
const handleClick = () => props.myOnClickFunction(props.increment); | |
return ( | |
<button onClick={onButtonClick}> | |
+{props.increment} | |
</button> | |
); | |
} | |
function Display(props){ | |
return (<label>{props.message}</label>) | |
} | |
function App(){ | |
const [counter, setCounter] = useState(10); | |
const increamentCounter = (incrementValue) => setCounter(counter+incrementValue); | |
return ( | |
<div> | |
<Button myOnClickFunction={increamentCounter} increment={1}/> | |
<Button myOnClickFunction={increamentCounter} increment={5}/> | |
<Button myOnClickFunction={increamentCounter} increment={10}/> | |
<Button myOnClickFunction={increamentCounter} increment={15}/> | |
<Display message={counter}/> | |
</div> | |
) | |
} | |
ReactDOM.render( | |
<App/>, | |
document.getElementById('mountNode'), | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment