react中reduce基本使用
import React ,{useReducer}from 'react';
import './App.css';
const App =() =>{
const reduce =(state,action)=>{
const actionFn = {
add:function(){
return {
...state,
count:state.count+1
}
}
}
return actionFn[action.type]();
}
const [state,dispatch] = useReducer(reduce,{count:0})
const addCount = () =>{
dispatch({type:'add'})
}
return (
<div>
<div>{state.count}</div>
<button onClick={addCount}>+1</button>
</div>
);
}
export default App;

浙公网安备 33010602011771号