通过 .
的方式进行具体 model 下的 action 派发,具体如下:
model
const count = {
namespace: 'count',
state: 0,
reducers: {
add(state, { payload }) {
return state + payload;
}
},
effects: {
* asyncAdd({ payload }, { put }, actionCreator) {
yield new Promise(resolve => setTimeout(resolve, 1000));
yield put(actionCreator.add(payload));
}
}
}
组件
import React from 'react';
import { connect, dispatch } from '@maoyan/tangdao';
function App(props) {
const { count } = props;
return (
<div>
当前计数器为:{count}
<button onClick={() => { dispatch.count.add(1); }}>increment</button>
<button onClick={() => { dispatch.count.asyncAdd(1); }}>asyncAdd</button>
</div>
)
}