1import React, { useState } from 'react';
2
3function Example() {
4 // Declare a new state variable, which we'll call "count"
5 const [count, setCount] = useState(0);
6 return (
7 <div>
8 <p>You clicked {count} times</p>
9 <button onClick={() => setCount(count + 1)}>
10 Click me
11 </button>
12 </div>
13 );
14}
1const [count, setCount] = React.useState(0);
2 const [count2, setCount2] = React.useState(0);
3
4 // increments count by 1 when first button clicked
5 function handleClick(){
6 setCount(count + 1);
7 }
8
9 // increments count2 by 1 when second button clicked
10 function handleClick2(){
11 setCount2(count2 + 1);
12 }
13
14 return (
15 <div>
16 <h2>A React counter made with the useState Hook!</h2>
17 <p>You clicked {count} times</p>
18 <p>You clicked {count2} times</p>
19 <button onClick={handleClick}>
20 Click me
21 </button>
22 <button onClick={handleClick2}>
23 Click me2
24 </button>
25 );
1import React, { useState } from 'react';
2
3function Example() {
4
5 const [count, setCount] = useState(0);
6
7 return (
8 <div>
9 <p>You clicked {count} times</p>
10 <button onClick={() => setCount(count + 1)}>
11 Click me
12 </button>
13 </div>
14 );
15}
1 1: import React, { useState } from 'react'; 2:
2 3: function Example() {
3 4: const [count, setCount] = useState(0); 5:
4 6: return (
5 7: <div>
6 8: <p>You clicked {count} times</p>
7 9: <button onClick={() => setCount(count + 1)}>10: Click me
811: </button>
912: </div>
1013: );
1114: }
1import React, { useState } from 'react';
2function Example() {
3 const [variable, callforupdate] = useState(defaulttwhatever);// <-- this it
4 return (
5 <div></div>
6 );
7}
1//initial state
2const [state, setState] = useState(0)
3//change state
4setState(1)
5//change state with prev state value
6setState((prevState) => {prevState + 2}) // 3