带React-Redux的套接字

时间:2018-09-17 19:35:34

标签: reactjs redux

我第一次在React-Redux中使用socket.io。

目前我正在做这样的事情

componentDidMount() {
  this.socket.on('trades', (tradeMsg) => {  
              if (this.updateCoinData[i]["short"] == tradeMsg.coin ) {
                  //Search for changed Crypto Value
                 this.updateCoinData[i]["perc"] = tradeMsg["message"]["msg"]["perc"]]

                  //Update the crypto Value state in Redux
                  this.props.updateCrypto(this.updateCoinData);

              }

this.props.updateCrypto(this.updateCoinData);在何处触发还原操作

export const updateCrypto = (updatedData) => {
  return function (dispatch) {
    dispatch({
      type: UPDATE_CRYPTO_DATA,
      payload: updatedData
    })
  }
}

在进行此项工作并更新状态时,我目前正在stateless component内进行操作,其中组件中的断开套接字将卸载

componentWillUnmount() {
 this.socket.disconnect();
}

现在,我意识到我也想在其他组件中使用它,因为我正在使用redux,所以我可以轻松地传输状态,但是当我移至其他组件时,我还要断开套接字的连接,这意味着我的数据已经赢得了不能实时更新。

[问题:] 那么,我如何创建套接字连接,以使它继续更新数据而与组件无关?如果我从this.socket.disconnect();中删除了componentWillUnmount,会行得通吗?

0 个答案:

没有答案