更新时间: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.