且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

React Apollo 和 Redux:将自定义减速器与 Apollo 状态相结合

更新时间:2022-11-18 19:15:02

我的解决方案是使用 recompose 通过 mapProps 将 store 和 Apollo 状态一起compose:

My solution was to use recompose to compose the store and Apollo states together via mapProps:

import { mapProps, compose } from ‘recompose’;

const mapStateToProps = state => ({
  selectedItem: state.ui.selectedItem
})

const mapDataToProps = {
  props: ({ data }) => ({ list: data.item.list })
}

const selectProps = mapProps(({ selectedItem, list}) => ({
  item: list[selectedItem] 
}))

export default compose(
  connect(mapStateToProps, {}),
  graphql(MY_QUERY, mapDataToProps),
  selectProps
)(MyComponent);

这还允许我们使用 graphql 加载状态rel="nofollow noreferrer">branch,所以在数据可用之前我们不会运行 selectProps.

This also allows us to check the graphql loading state using branch, so we don't run selectProps until the data is available.

感谢 Daniel Rearden 的回答为我指明了正确的方向.

Thanks to Daniel Rearden's answer for pointing me in the right direction.