【发布时间】:2017-11-15 11:46:00
【问题描述】:
我有一个 react-redux 应用程序,它使用 Apollo 客户端对我的 api GraphQL 进行查询。
我有一个 GridContainer,它使用 export default connect(mapStateToProps, mapDispatchToProps)(GridContainerWithData); 进行查询调用,其中 GridContainerWithData 是一个 const,它使用 graphql(gql `...)
这会在我的 props 中返回 data,其中包含:查询响应、是否有错误以及加载状态(true 或 false)。
现在,我正在使用 if(loading) {} 自己管理加载。
这里的问题是我的组件在它处于加载状态时呈现,然后在查询完成加载时再次呈现。如果我不使用if(loading) {},我会收到一条错误消息,提示我需要在查询处于加载状态时返回一些内容。
有没有办法告诉组件在查询加载时不渲染?
我尝试过使用
shouldComponentUpdate (nextProps) {
if (this.props.data.loading !== nextProps.data.loading) {
console.log(‘i will rerender’, nextProps.data.loading);
return true;
}else {
return false;
}
}
但没有运气。
有没有办法告诉组件在查询加载时不渲染?
编辑:
如果它可以帮助任何人,这就是我的 GridContainer。
class ActionsToDoGridContainer extends Component {
render () {
const {error} = this.props.data;
if (error) {
return (
<div className='ComponentDistance'>
An unexpected error happened :
{error}
</div>);
} else {
return (
<div className='ComponentDistance'>
<ActionsToDoGrid {...this.props}/>
ActionsToDoButtons {...this.props}/>
</div>
);
}
}
}
【问题讨论】:
标签: reactjs redux react-redux apollo