【发布时间】:2017-08-03 12:57:06
【问题描述】:
我正在构建一个 react/meteor 应用程序。我在订阅方面遇到问题。当 subscriotion.ready() 为假时,会显示一个组件。当它变为 true 时,组件将被一个包含数据的表替换,但在准备就绪和 find().fetch() 中的数据之间需要几秒钟,显示另一个组件一段时间。
有什么建议吗?
谢谢
【问题讨论】:
我正在构建一个 react/meteor 应用程序。我在订阅方面遇到问题。当 subscriotion.ready() 为假时,会显示一个组件。当它变为 true 时,组件将被一个包含数据的表替换,但在准备就绪和 find().fetch() 中的数据之间需要几秒钟,显示另一个组件一段时间。
有什么建议吗?
谢谢
【问题讨论】:
如果您使用react-meteor-data,您可以在ready 属性中获得subscription 状态。然后您可以将此属性发送到演示组件并进行相应的更新。
包文档中的示例代码 sn-p:
import { createContainer } from 'meteor/react-meteor-data';
export default PresenterContainer = createContainer(props => {
// Do all your reactive data access in this method.
// Note that this subscription will get cleaned up when your component is unmounted
const handle = Meteor.subscribe('publication_name');
return {
isReady: ! handle.ready(),
list: CollectionName.find().fetch(),
};
}, PresenterComponent);
说明:
createContainer 的第一个参数是一个反应函数,只要它的反应输入发生变化就会重新运行。
PresenterComponent 组件将接收 {isReady, list} 作为道具。所以,你可以根据isReady的状态来渲染你的组件
加法:
像这样编写演示者组件的 render 方法:
render(){
if(!this.isReady) return <LoadingComponent/>
else if(this.props.list.length() != 0) return <TableComponent/>
else return <NoDataFoundComponent/>
}
【讨论】: