Reactjs socket.io-client重定向到redux thunk中间件
我正在使用 我正在从server.js将一个操作从后端服务器发送到客户端Reactjs socket.io-client重定向到redux thunk中间件,reactjs,redux,socket.io,react-redux,redux-thunk,Reactjs,Redux,Socket.io,React Redux,Redux Thunk,我正在使用 我正在从server.js将一个操作从后端服务器发送到客户端 soc.emit('action', { type: 'SOCKET_TAG', data: tag }); 在store.js中,我正在使用socket io中间件创建一个存储 import { createStore, applyMiddleware } from "redux"; ... import createSocketIoMiddleware from 'redux-socket.io'; import i
soc.emit('action', { type: 'SOCKET_TAG', data: tag });
在store.js中,我正在使用socket io中间件创建一个存储
import { createStore, applyMiddleware } from "redux";
...
import createSocketIoMiddleware from 'redux-socket.io';
import io from 'socket.io-client';
let socket = io('ws://127.0.0.1:4000');
let socketIoMiddleware = createSocketIoMiddleware(socket, "server/");
let store = createStore(rootReducer, applyMiddleware(thunk, logger, socketIoMiddleware));
...
实际上,当我从后端发出套接字操作socket_标记时,该操作将在userReducer中执行
const userReducer = (state = initState, action) => {
switch (action.type) {
case actionTypes.SOCKET_TAG:
console.log(actionTypes.SOCKET_TAG, action.data)
return state;
default:
return state
}
但是我想在中间件中执行这个
export const initUser = () => {
const db = client.getServiceClient(RemoteMongoClient.factory, "mongodb-atlas").db(dbName);
return async (dispatch, getState) => { // async call to database
let user; // User found with the tag
client.auth.loginWithCredential(new AnonymousCredential()).then(u =>
user = db.collection(collectionName).findOne({ tag })
).then(() => {
dispatch({ type: actionTypes.INIT_USER, user });
}).catch((err) => {
dispatch({ type: actionTypes.ERROR, err });
});
}
}
我是redux thunk的新手,谢谢你我找到了一个解决方案,它可以使用,对我来说它可以工作,但我认为这不是最好的方法。