您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

如何在组件外部调度redux动作?

如何在组件外部调度redux动作?

如果您在启动时初始化套接字客户端,只需将您的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

其他 2022/1/1 18:13:39 有492人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶