Javascript: Difference between revisions
Jump to navigation
Jump to search
Created page with "=react= Normal function <syntaxhighlight lang="javascript"> function logRandom() { console.log(Math.random()); } function Button() { const [count, setCounter] = useS..." |
No edit summary |
||
Line 1: | Line 1: | ||
=react= | =react functions and state= | ||
Normal function | Normal function | ||
Line 44: | Line 44: | ||
return <button onClick={() = > console.log(Math.random())}>{counter}</button>; | return <button onClick={() = > console.log(Math.random())}>{counter}</button>; | ||
} | } | ||
</syntaxhighlight> | |||
=Passing Props= | |||
<syntaxhighlight lang="javascript"> | |||
function Button(props) { | |||
// const handleClick = () => setCounter(counter+1); | |||
return ( | |||
<button onClick={props.onClickFunction}> | |||
+{props.increment} | |||
</button> | |||
); | |||
} | |||
function Display(props) { | |||
return ( | |||
<div>{props.message}</div> | |||
); | |||
} | |||
function App() { | |||
const [counter, setCounter] = useState(0); | |||
const incrementCounter = () => setCounter(counter+1); | |||
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'), | |||
); | |||
</syntaxhighlight> | </syntaxhighlight> |
Revision as of 01:51, 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 = () => setCounter(counter+1);
return (
<button onClick={props.onClickFunction}>
+{props.increment}
</button>
);
}
function Display(props) {
return (
<div>{props.message}</div>
);
}
function App() {
const [counter, setCounter] = useState(0);
const incrementCounter = () => setCounter(counter+1);
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'),
);