Using the State Hook
xxxxxxxxxx
import React, { useState } from 'react';
function Example() {
// Declare a new state variable, which we'll call "count"
const [count, setCount] = useState(0);
return (
<div>
<p>You clicked {count} times</p>
<button onClick={() => setCount(count + 1)}>
Click me
</button>
</div>
);
}
xxxxxxxxxx
import React, { useState } from 'react'
import { render } from 'react-dom'
const randomDiceRoll = () => {
return Math.floor(Math.random() * 6) + 1
}
export default function App() {
const [diceRolls, setDiceRolls] = useState([1, 2, 3])
return (
<div>
<button
onClick={() => {
setDiceRolls([diceRolls, randomDiceRoll()])
}}
>
Roll dice
</button>
<ul>
{diceRolls.map((diceRoll, index) => (
<li key={index}>{diceRoll}</li>
))}
</ul>
</div>
)
}
render(<App />, document.querySelector('#app'))
xxxxxxxxxx
const [count, setCount] = React.useState(0);
const [count2, setCount2] = React.useState(0);
// increments count by 1 when first button clicked
function handleClick(){
setCount(count + 1);
}
// increments count2 by 1 when second button clicked
function handleClick2(){
setCount2(count2 + 1);
}
return (
<div>
<h2>A React counter made with the useState Hook!</h2>
<p>You clicked {count} times</p>
<p>You clicked {count2} times</p>
<button onClick={handleClick}>
Click me
</button>
<button onClick={handleClick2}>
Click me2
</button>
);
xxxxxxxxxx
// SampleButton.js
import { useState } from 'react';
export default function SampleButton() {
const [count, setCount] = useState(0) // count is initialized to 0
function handleClick() {
setCount(count + 1);
}
return (
<button onClick={handleClick}>
Times clicked: {count} {/* label updates after each click */}
</button>
)
}
// Documentation: https://react.dev/learn/state-a-components-memory
xxxxxxxxxx
1: import React, { useState } from 'react';
2:
3: function Example() {
4: const [count, setCount] = useState(0);
5:
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: }
xxxxxxxxxx
import React, { useState } from 'react';
function Example() {
// Declare a new state variable, which we'll call "count"
const [count, setCount] = useState(0);
return (
<div>
<p>You clicked {count} times</p>
<button onClick={() => setCount(count + 1)}>
Click me
</button>
</div>
);
}
xxxxxxxxxx
import React, { useState } from 'react';
function Example() {
const [count, setCount] = useState(0);
return (
<div>
<p>You clicked {count} times</p>
<button onClick={() => setCount(count + 1)}>
Click me
</button>
</div>
);
}
xxxxxxxxxx
//The useState Hook
//useState is a Hook that lets you
//add React state to function components.
//after a state is changed, the page is rerendered, refecting
//the state change
import React, { useState } from 'react';
function Example() {
// here we declare the state variable count
//and its function or 'setter', setCount
const [count, setCount] = useState(0);
//It returns a pair of values: the current state(count)
//and a function that updates it(setCount)
return (
<div>
<p>You clicked {count} times</p>
//{*setCount is the function that updates the value*}
<button onClick={() => setCount(count + 1)}>
Click me
</button>
</div>
);
}
xxxxxxxxxx
// Hooks are a new addition in React 16.8. They let you use state and other React features without writing a class.
import React, { useState } from 'react';
function Example() {
// Declare a new state variable, which we'll call "count"
const [count, setCount] = useState(0);
return (
<div>
<p>You clicked {count} times</p>
<button onClick={() => setCount(count + 1)}>
Click me
</button>
</div>
);
}
xxxxxxxxxx
function Counter({initialCount}) {
const [count, setCount] = useState(initialCount);
return (
<>
Count: {count}
<button onClick={() => setCount(initialCount)}>Reset</button>
<button onClick={() => setCount(prevCount => prevCount - 1)}>-</button>
<button onClick={() => setCount(prevCount => prevCount + 1)}>+</button>
</>
);
}