如果您在启动时初始化套接字客户端,只需将您的redux存储传递到函数中,然后使用 store.dispatch()
export function createSocket (url, store) {
console.log(`connecting to ${url}`)
store.dispatch(actions.CONNECT)
return io(url)
}
如果不是在启动时创建的,则套接字创建应该由某些用户操作(或类似操作)触发,在这种情况下,您应该能够使用redux-thunk
中间件获取对调度功能的引用:
//the action returns a function, which gets called with store.dispatch as an argument
const myAction = someArg => dispatch => {
createSocket(url, dispatch);
};
有关详细信息,请参见https://github.com/reduxjs/redux- thunk