在react / redux中的store.subscribe中调用一个函数

时间:2018-12-26 13:43:13

标签: javascript reactjs redux

我想在store.subscribe内调用一个函数,但总是收到错误消息:undefined this

这是我正在尝试的代码:

componentDidMount() {
  this.GetFilteredData();
}

GetFilteredData = () => {
  WithHoldTaxApi.GetFilteredData()
    .then((collection) => {
      console.log("data", collection)
      this.setState({ collection: collection })
    }
    ).catch(error => {
      throw error;
    })
}

store.subscribe(() => {
  console.log(store.getState())
  this.GetFilteredData();
})

1 个答案:

答案 0 :(得分:0)

我认为您需要执行以下操作:

componentDidMount() {
  this.subscribe();
}

GetFilteredData = () => {
  WithHoldTaxApi.GetFilteredData()
    .then((collection) => {
      console.log("data", collection)
      this.setState({ collection: collection })
    }
    ).catch(error => {
      throw error;
    })
}

subscribe = () => {
  store.subscribe(() => {
     console.log(store.getState())
     this.GetFilteredData();
  })
}