redux未拾取通过操作分派的对象
我在redux未拾取通过操作分派的对象,redux,dispatch,redux-saga,Redux,Dispatch,Redux Saga,我在sagas.jsas中创建了一个rootSaga function* fetchStuff(action) { try { yield put({type: 'INCREMENT'}) yield call(delay, 1000) yield put({type: 'DECREMENT'}) const highlights = yield call(API.getStuff, action.data.myObject); }
sagas.js
as中创建了一个rootSaga
function* fetchStuff(action) {
try {
yield put({type: 'INCREMENT'})
yield call(delay, 1000)
yield put({type: 'DECREMENT'})
const highlights = yield call(API.getStuff, action.data.myObject);
} catch (e) {
yield put({type: 'FETCH_STUFF_FAILED', message: e});
}
}
export default function* rootSaga() {
yield takeEvery('INIT_LOAD', fetchStuff);
}
我在第三方之后调用INIT\u LOAD
。方法
:
class myClass extends Component {
constructor(props) {
super(props);
}
componentDidMount() {
this.load();
}
load = () => {
this.init = () => {
this.myObject = thirdParty.method(event => {
const action = {
type: 'INIT_LOAD',
payload: {
myObject: this.myObject
}
};
store.dispatch(action);
});
};
this.init();
};
render() {
return (
<div id="render-here" />
);
}
为什么我无法传递this.myObject
但字符串可以
更新:这不是一个传奇问题。我用纯redux复制了相同的问题。rootReducer
as
export default function rootReducer(state = initialState, action) {
switch (action.type) {
case 'INIT_LOAD':
return Object.assign({}, state, { myObject: action.payload.myObject });
default:
return state;
}
}
正如我在下面的评论中提到的,将其分配给对象Obj
不会改变问题
let Obj = {};
...
load = () => {
this.init = () => {
Obj.myObject = thirdParty.method(event => {
const action = {
type: 'INIT_LOAD',
payload: {
myObj: Obj
}
};
store.dispatch(action);
});
};
this.init();
};
更新2
我清理了代码&只是在组件中发送了一个触发事件的操作。在这个传奇故事中,我做了
init()
。我遇到了另一个问题,我试图保存在redux存储中的对象具有活动套接字会话(这些会话是跨域的问题)。虽然我没有解决最初的问题,但没有存储套接字对象使我的问题消失了。从我看到,您正在将this.myObject
分配给thirdParty.method()
,但是您也在thirdParty.method()回调中的action const中使用this.myObject
。因此,在给定实际值之前,您正在使用this.myObject
。你希望这个.myObject
有什么值?我不完全确定这是真的。如果我这样做:让myObject={}
&然后myObject.myMethod=thirdParty.method(…)
&然后在我的操作中负载:{myObject}
,这个传奇仍然没有被触发。
let Obj = {};
...
load = () => {
this.init = () => {
Obj.myObject = thirdParty.method(event => {
const action = {
type: 'INIT_LOAD',
payload: {
myObj: Obj
}
};
store.dispatch(action);
});
};
this.init();
};