简体   繁体   English

在我的 redux 商店中将我的插座放在哪里?

[英]Where to hold my socket in my redux store?

Currently I am using slices and redux-thunks to create my redux store.目前我正在使用切片和 redux-thunk 来创建我的 redux 存储。 I am fairly new to redux and I quickly realized that state is not the place to store my socket connection.我对 redux 还很陌生,我很快意识到 state 不是存储我的套接字连接的地方。 The socket connection is global and is used by almost all of the components.套接字连接是全局的,几乎所有组件都使用它。 And most importantly the socket connection needs to stay open, so I can't be making a new connection on each dispatched action.最重要的是套接字连接需要保持打开状态,所以我不能在每个调度的动作上建立新的连接。

Any advice, suggestions, or examples would be much appreciated.任何建议、建议或示例将不胜感激。

Websockets should typically go in a Redux middleware, where they have access to dispatch and getState : Websockets 通常应该在 Redux 中间件中 go ,它们可以访问dispatchgetState

https://redux.js.org/faq/code-structure#where-should-websockets-and-other-persistent-connections-live https://redux.js.org/faq/code-structure#where-should-websockets-and-other-persistent-connections-live

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

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