我有一个High Order Component,可以在其中传递一个graphql查询定义数组。此HOC还包括renderComponent from react-apollo recompose,以确保如果graphql查询仍在运行,则该组件不会呈现。我遇到的问题是,当我链接graphql查询时,它将在第二个查询完成之前呈现组件。因此,我假设第一个查询完成,然后将props.data.loading
变量设置为false
,然后呈现该组件。但是,第二个查询永远不会结束。这是我的高阶组件:
const ErrorComponent = ({ error }) => (
<span>
Something went wrong: {JSON.stringify(error)}
</span>
);
export function asContainer(Component, mapStateToProps, mapDispatchToProps, graphqlQueries = [], lifecycleSpec = {}) {
const renderWhileLoading = () =>
branch(
props => props['data'] && props['data'].loading,
renderComponent(Loader),
);
const renderForError = () =>
branch(
props => props['data'] && props['data'].error,
renderComponent(ErrorComponent),
);
let parts = [connect(mapStateToProps, mapDispatchToProps)];
if (graphqlQueries.length > 0) {
for (let i = 0; i < graphqlQueries.length; i++) {
let g = graphqlQueries[i];
parts.push(graphql(g.query, g.opts))
}
}
parts.push(
lifecycle(lifecycleSpec),
renderWhileLoading(),
renderForError(),
pure
);
return compose(parts)(Component);
}
使用容器模式,这就是调用此HOC的组件:
export const AuthedRouterContainer = asContainer(
AuthedRouter,
mapStateToProps,
mapDispatchToProps,
[{
name: "user",
query: LoggedInUserQuery,
opts: {
options: (props) => ({
variables: {
id: props.currentUser.id
}
}),
}
}, {
query: GetEntityProcessesQuery,
opts: {
name: "processes",
options: (props) => ({
variables: {
entityId: props.currentEntity.id
}
}),
skip: (props) => !props.currentEntity
}
}]
);
我需要帮助弄清楚为什么在将组件传递到HOC之前,它不等待第二个查询完成。