import React, { useReducer } from 'react';

function ReducerDemo(){
const [ count , dispatch] =useReducer((state,action)=>{
//判断action类型
switch(action){
case 'add':
return state+1
case 'sub':
return state-1
default:
return state
}
},0) //当前count

return(
<div>
<h2>现在的数字是{count}</h2>
<button onClick={()=>dispatch('add')}>加</button>
<button onClick={()=>dispatch('sub')}>减</button>
</div>
)
}

export default ReducerDemo