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>
);
}
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'))
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>
);
}
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>
</>
);
}
import { useState } from "react"