javascript - 在你的 redux 应用程序中哪里有套接字对象?

标签 javascript reactjs sockets redux socket.io

<分区>

我正在使用 React、redux 和 socket.io 开发聊天应用程序。正如我们所知,redux 非常有助于将一些状态传递给深层和嵌套的组件。我喜欢这个想法,将一些 Prop 存储为 redux 状态,这些状态将传递给一些嵌套很深的组件。我正在使用 socket.io-client用于创建套接字对象的库。

我需要将创建的套接字对象传递给一些嵌套很深的组件,因此,我考虑在 redux 状态下创建套接字对象,以便我可以在嵌套组件上轻松使用它。

这是一个好方法还是我应该做其他事情?

最佳答案

不,不是。

我们最近添加了一个 Redux FAQ entry on where websockets should live in a Redux app .引用该条目:

Middleware are the right place for persistent connections like websockets in a Redux app, for several reasons:

  • Middleware exist for the lifetime of the application
  • Like with the store itself, you probably only need a single instance of a given connection that the whole app can use
  • Middleware can see all dispatched actions and dispatch actions themselves. This means a middleware can take dispatched actions and turn those into messages sent over the websocket, and dispatch new actions when a message is received over the websocket.
  • A websocket connection instance isn't serializable, so it doesn't belong in the store state itself

关于javascript - 在你的 redux 应用程序中哪里有套接字对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52345057/

相关文章:

javascript - 使用 JSOM 在 SharePoint 列表功能区按钮中添加按钮

javascript - 当文本选择达到 3 个字母长度时,防止在文本框中退格

javascript - 从 React-Select 组件获取所选值的最佳方法是什么?

javascript - 将动态 onChange 监听器传递给 child

c# - 通过套接字发送 inkcanvas 笔划

javascript - 同时拖动 2 个 Google map 标记

javascript - 如何从父级更改iframe内的网页样式

javascript - 如何创建线连接 mxGraph

Java套接字编程

Android 蓝牙 socket.connect() 失败