我正在尝试通过使用 redux watch 订阅更改来监视 redux 存储属性中的更改。 对于一个属性,它工作正常。 另一方面,我正在化简器中发出 API 请求(无法在操作中发出异步请求(。
我已经通过创建一个按钮来显示新信息来验证 redux 状态确实会更新。但是,我的组件实际上并没有订阅更改。有谁知道这是为什么?
import watch from 'redux-watch';
import {store} from '../index.js';
class ListResults extends Component {
constructor() {
super();
this.state = {
profiles: [],
};
}
componentDidMount() {
let w = watch(store.getState, 'searchResults');
store.subscribe(w((newVal, oldVal, objectPath) => {
// not getting called
console.log("Successfully got new searchResults")
}))
}
...
render() {
...
}
...
function mapStateToProps(state) {
return {
searchResults: state.searchResults,
};
}
export default connect(mapStateToProps)(ListResults);
Redux reducer 必须是纯函数!
。在化简器中发出 API 请求(无法在操作中发出异步请求(。
听起来很像不正确的减速器实现。
为了使用 redux 发出异步请求,您应该研究 redux 中间件,如 redux-thunk
、redux-observables
或 redux-saga