Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我正在将 Redux 与 react 一起使用,但遇到了一个问题。在我的应用程序中,我通过 websockets 收听来自 BE 的消息。当应用程序加载时,它应该监听发送的 websocket 消息并调度一些操作。
我怎么做 ?
您可以在组件加载时开始监听 Web 套接字事件。传递该dispatch方法,以便一旦有数据就可以调度操作。
dispatch
@connect(state => ({})) class MainComponent extends Component { componentDidMount() { socket.onmessage = (e) => { this.props.dispatch({ type: 'SOCKET_DATA', payload: e.data }); }; } }