Javascript: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
|||
Line 52: | Line 52: | ||
function Button(props) { | function Button(props) { | ||
const handleClick = () => props.onClickFunction(props.increment); | |||
return ( | return ( | ||
<button onClick={ | <button onClick={handleClick}> | ||
+{props.increment} | +{props.increment} | ||
</button> | </button> | ||
Line 68: | Line 68: | ||
function App() { | function App() { | ||
const [counter, setCounter] = useState(0); | const [counter, setCounter] = useState(0); | ||
const incrementCounter = () => setCounter(counter+ | const incrementCounter = (incrementValue) => setCounter(counter+incrementValue); | ||
return ( | return ( | ||
<div> | <div> | ||
Line 84: | Line 84: | ||
document.getElementById('mountNode'), | document.getElementById('mountNode'), | ||
); | ); | ||
</syntaxhighlight> | </syntaxhighlight> |
Revision as of 01:56, 19 May 2020
react functions and state
Normal function
function logRandom() {
console.log(Math.random());
}
function Button() {
const [count, setCounter] = useState(0);
return <button onClick={logRandom}>{counter}</button>;
}
inline function
function Button() {
const [count, setCounter] = useState(0);
return <button onClick={
function logRandom() {
console.log(Math.random());
}
}>{counter}</button>;
}
inline arrow function definition
function Button() {
const [count, setCounter] = useState(0);
return <button onClick={() = > console.log(Math.random())}>{counter}</button>;
}
Passing Props
function Button(props) {
const handleClick = () => props.onClickFunction(props.increment);
return (
<button onClick={handleClick}>
+{props.increment}
</button>
);
}
function Display(props) {
return (
<div>{props.message}</div>
);
}
function App() {
const [counter, setCounter] = useState(0);
const incrementCounter = (incrementValue) => setCounter(counter+incrementValue);
return (
<div>
<Button onClickFunction={incrementCounter} increment={1} />
<Button onClickFunction={incrementCounter} increment={5} />
<Button onClickFunction={incrementCounter} increment={10} />
<Button onClickFunction={incrementCounter} increment={100} />
<Display message={counter}/>
</div>
);
}
ReactDOM.render(
<App />,
document.getElementById('mountNode'),
);