useState とコンポーネントの切り分け方 | React初歩
useState の使い方
useState とコンポーネントの切り分け方
prev は 更新用関数 という
code:javascript
import { useState } frmo 'react'
const App = () => {
return (
<>
<CountResult title='カウント' count={count} />
<CountUpdate setCount={setCount} />
</>
)
}
const CountResult = (title, count) => <h3>{title}: {count}</h3>
const CountUpdate = (setCount) => {
const countUp = () => {
setCount(prev => prev + 1)
}
const countDown = () => {
setCount(prev => prev - 1)
}
return (
<>
<button onClick={countUp}>+1</button>
<button onClick={countDown}>-1</button>
</>
)
}