ita*_*ied 4 javascript reactjs react-native redux
我正在使用redux
我的react-native
移动应用程序.
与后端的通信是通过websockets使用socket.io
.
我以一种方式组织项目,我有一个文件(你可以称之为对象),初始化套接字客户端.
处理程序已注册到它,我想向其发送操作reducers
.
socket.js
:
export function createSocket (url) {
console.log(`connecting to ${url}`)
// dispatch(actions.CONNECT)
return io(url)
}
Run Code Online (Sandbox Code Playgroud)
如何dispatch
从这些功能之外采取行动react component
呢?
如果在启动时初始化套接字客户端,只需将redux存储传递给函数,然后使用 store.dispatch()
export function createSocket (url, store) {
console.log(`connecting to ${url}`)
store.dispatch(actions.CONNECT)
return io(url)
}
Run Code Online (Sandbox Code Playgroud)
如果不是在启动时,则应该通过某些用户操作(或类似的东西)触发您的套接字创建,在这种情况下,您应该能够使用redux-thunk
中间件获取对调度函数的引用:
//the action returns a function, which gets called with store.dispatch as an argument
const myAction = someArg => dispatch => {
createSocket(url, dispatch);
};
Run Code Online (Sandbox Code Playgroud)
有关详细信息,请参阅https://github.com/reduxjs/redux-thunk
归档时间: |
|
查看次数: |
5081 次 |
最近记录: |