Redux 和 Socket IO 代理错误



我正在使用以下插件来管理我的套接字IO实例:

https://github.com/itaylor/redux-socket.io

我只能在用户登录后才能够初始化套接字。插件建议在存储设置时设置套接字实例中的插件:

import { createStore, applyMiddleware } from 'redux';
import createSocketIoMiddleware from 'redux-socket.io';
import io from 'socket.io-client';
let socket = io('http://localhost:3000');
let socketIoMiddleware = createSocketIoMiddleware(socket, "server/");
function reducer(state = {}, action){
  switch(action.type){
    case 'message':
      return Object.assign({}, {message:action.data});
    default:
      return state;
  }
}
let store = applyMiddleware(socketIoMiddleware)(createStore)(reducer);
store.subscribe(()=>{
  console.log('new client state', store.getState());
});
store.dispatch({type:'server/hello', data:'Hello!'});

由于我无法同时设置该实例,因此我需要设置商店,我需要找到一种稍后设置的方法。我在redux-socket.io问题中找到了这个解决方案:

https://github.com/itaylor/redux-socket.io/issues/13#issuecomment-256095866

似乎是一个完美的解决方案,所以我使用当前的商店设置实现了它:

import {applyMiddleware,createStore} from 'redux';
import rootReducer from './reducers/___index';
import thunk from 'redux-thunk';
let socketIoMiddleware = null;
const proxyMiddleware = store => next => action => {
  if (socketIoMiddleWare !== null) {
     return socketIoMiddleware(store)(next)(action);
  }
  return next(action);
}
const middlewares = [proxyMiddleware, thunk];
const store = createStore(rootReducer,applyMiddleware(...middlewares));

商店毫无疑问地使商店呈罚款,但是当我尝试通过单击按钮来更改商店状态的任何内容时,我会收到以下错误:

socketiomiddleware未定义

我在redux-socket.io repo中发现的上述解决方案似乎对他人有用,所以我不确定为什么要遇到此错误。

任何建议都将不胜感激!

这里的 if(socketIoMiddleWare !== null) {应该为 socketIoMiddleware。看起来像错字

最新更新