简体   繁体   English

在接收套接字请求中使用redux thunk和socket.io

[英]Using redux thunk and socket.io in receiving socket request

I'm using redux thunk and socket.io, I have little clue about handling the socket io, for emit I put in action, but how about receiving? 我正在使用redux thunk和socket.io,对于处理套接字io我几乎一无所知,因为我付诸行动了,但是接收又如何呢? I tried below approach in my reducer file. 我在我的reducer文件中尝试了以下方法。

const store =
  getUserRole() === 'member' ? import('../store') : import('../adminStore')

socket.on('receive-payment', function(payload) {
    store.dispatch({
      type: ADD_PAYMENT,
      payload
    })
})

I got error of store.dispatch is not a function . 我收到store.dispatch is not a function错误store.dispatch is not a function What's wrong here? 怎么了

I'm not sure but I think you should probably first define these two imports in it's variables and then assign one or the other one in the store. 我不确定,但我认为您可能应该首先在变量中定义这两个导入,然后在商店中分配一个或另一个。

import store as store1 from '../store';
import store as store2 from '../adminStore';

const store = getUserRole() === 'member' ? store1 : store2;

socket.on('receive-payment', function(payload) {
    store.dispatch({
      type: ADD_PAYMENT,
      payload
    })
})

Please let me know if this helps. 请让我知道这可不可以帮你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM