简体   繁体   English

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

[英]Call a function inside store.subscribe in react/redux

I want to call a function inside a the store.subscribe and i always get an error: undefined this , 我想在store.subscribe调用一个函数,但我总是收到一个错误: undefined this

This is the code that i was trying : 这是我正在尝试的代码:

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();
})

I think you will need to do something like this: 我认为您将需要执行以下操作:

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();
  })
}

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

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