Skip to content

Instantly share code, notes, and snippets.

@DaddyMoe
Created January 10, 2020 22:32
Show Gist options
  • Save DaddyMoe/b0b8e91765e0d0ad259483c12fde37bd to your computer and use it in GitHub Desktop.
Save DaddyMoe/b0b8e91765e0d0ad259483c12fde37bd to your computer and use it in GitHub Desktop.
React state management and Components reusing
// 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