useReducer():Action钩子
import React, {useReducer} from 'react'
export default function UseReducer() {
const reducer = (state, action) => {
switch (action.type) {
case 'add':
return {count: state.count + 1};
default:
return {count: state.count};
}
}
const [state, dispatch] = useReducer(reducer, {count: 0})
const addcount = () => {
dispatch({
type: 'add'
})
}
return (
<div>
<p>{state.count}</p>
<button onClick={addcount}>count++</button>
</div>
)
}