data.loading
,但在大多数情况下,检查查询结果是否为null也同样有效:
// Should probably check this first. If you error out, usually your data will be
// undefined, which means putting this later would result in it never getting
// called. Also checking if it's not-null is a bit redundant :)
if (this.props.TestData.error) return <div>Error...</div>
// `testList` will only be undefined during the initial fetch
// or if the query errors out
if (!this.props.TestData.getTestData) return <div>Loading...</div>
// Render the component as normal
return <table>...</table>
还请记住,GraphQL可能返回一些错误,并且数据仍将返回。这意味着在生产环境中,您可能需要更健壮的错误处理行为,如果存在任何错误,则不一定会阻止页面呈现。