我可以在使用 redux-actions 时提前绑定参数值吗?
Can I bind an argument value ahead of time when using redux-actions?
我有一个动作和一个传奇。
我想从 actions.js
内部为动作实例提供一个值。
我还想从 saga 中为动作实例提供一个值。
因此,当调用与 action 关联的 reducer 时,它会在有效负载中接收到两个值。
我该怎么做?
actions.js
import { createAction } from 'redux-actions';
export const myAction = createAction('ACTION_NAME'); // How do I supply payload value "foo" here?
sagas.js
function* mySaga({
yield put(myAction()); // How do I supply payload value "bar" here?
}
reducers.js
export default handleActions({
'ACTION_NAME': handleReduce(state, { payload: { foo, bar } }) =>
{ /* foo and bar should be defined */ }) });
当您创建一个动作时,您可以向其中添加您想要的任何负载。现在您只需使用裸字符串创建操作,没有有效负载。
createAction(type, payloadCreator)
payloadCreator 必须是函数、未定义或 null
createAction('ACTION_NAME', () => { reuturn {foo: 'bar'}; })
在 sagas put 中也接受类型为(字符串)和负载的裸对象
例如yield put({ type: 'PRODUCTS_RECEIVED', products })
因此,如果 createAction returns 具有类型和负载的对象,它也会被 sagas 使用。您可以在 yeld 之前使用 console.log 来检查您的动作创建者 returns.
console.log('Action for saga:', myAction());
yield put(myAction()); // How do I supply payload value "bar" here?
我有一个动作和一个传奇。
我想从 actions.js
内部为动作实例提供一个值。
我还想从 saga 中为动作实例提供一个值。
因此,当调用与 action 关联的 reducer 时,它会在有效负载中接收到两个值。
我该怎么做?
actions.js
import { createAction } from 'redux-actions';
export const myAction = createAction('ACTION_NAME'); // How do I supply payload value "foo" here?
sagas.js
function* mySaga({
yield put(myAction()); // How do I supply payload value "bar" here?
}
reducers.js
export default handleActions({
'ACTION_NAME': handleReduce(state, { payload: { foo, bar } }) =>
{ /* foo and bar should be defined */ }) });
当您创建一个动作时,您可以向其中添加您想要的任何负载。现在您只需使用裸字符串创建操作,没有有效负载。
createAction(type, payloadCreator)
payloadCreator 必须是函数、未定义或 null
createAction('ACTION_NAME', () => { reuturn {foo: 'bar'}; })
在 sagas put 中也接受类型为(字符串)和负载的裸对象
例如yield put({ type: 'PRODUCTS_RECEIVED', products })
因此,如果 createAction returns 具有类型和负载的对象,它也会被 sagas 使用。您可以在 yeld 之前使用 console.log 来检查您的动作创建者 returns.
console.log('Action for saga:', myAction());
yield put(myAction()); // How do I supply payload value "bar" here?