我将其设置为使您依靠全局状态变量来告诉组件何时渲染。在许多组件相互通信的情况下,Redux更好,您在注释中提到有时使用它。因此,我将使用Redux勾勒出一个答案。
您必须将API调用移至父容器Component A
。如果您想让子孙仅在API调用完成后呈现,则不能将这些API调用保留在子孙本身中。如何从尚不存在的组件进行API调用?
完成所有API调用后,您可以使用操作来更新包含一堆数据对象的全局状态变量。每次接收到数据(或捕获到错误)时,您都可以调度操作以检查数据对象是否已完全填写。完全填写完毕后,您可以将loading
变量更新为false
,并有条件地渲染Grid
组件。
因此,例如:
// Component A
import { acceptData, catchError } from '../actions'
class ComponentA extends React.Component{
componentDidMount () {
fetch('yoururl.com/data')
.then( response => response.json() )
// send your data to the global state data array
.then( data => this.props.acceptData(data, grandChildNumber) )
.catch( error => this.props.catchError(error, grandChildNumber) )
// make all your fetch calls here
}
// Conditionally render your Loading or Grid based on the global state variable 'loading'
render() {
return (
{ this.props.loading && <Loading /> }
{ !this.props.loading && <Grid /> }
)
}
}
const mapStateToProps = state => ({ loading: state.loading })
const mapDispatchToProps = dispatch => ({
acceptData: data => dispatch( acceptData( data, number ) )
catchError: error=> dispatch( catchError( error, number) )
})
// Grid - not much going on here...
render () {
return (
<div className="Grid">
<GrandChild1 number={1} />
<GrandChild2 number={2} />
<GrandChild3 number={3} />
...
// Or render the granchildren from an array with a .map, or something similar
</div>
)
}
// Grandchild
// Conditionally render either an error or your data, depending on what came back from fetch
render () {
return (
{ !this.props.data[this.props.number].error && <Your Content Here /> }
{ this.props.data[this.props.number].error && <Your Error Here /> }
)
}
const mapStateToProps = state => ({ data: state.data })
您的化简器将持有全局状态对象,该对象将说明是否一切准备就绪:
// reducers.js
const initialState = {
data: [{},{},{},{}...], // 9 empty objects
loading: true
}
const reducers = (state = initialState, action) {
switch(action.type){
case RECIEVE_SOME_DATA:
return {
...state,
data: action.data
}
case RECIEVE_ERROR:
return {
...state,
data: action.data
}
case STOP_LOADING:
return {
...state,
loading: false
}
}
}
在您的操作中:
export const acceptData = (data, number) => {
// First revise your data array to have the new data in the right place
const updatedData = data
updatedData[number] = data
// Now check to see if all your data objects are populated
// and update your loading state:
dispatch( checkAllData() )
return {
type: RECIEVE_SOME_DATA,
data: updatedData,
}
}
// error checking - because you want your stuff to render even if one of your api calls
// catches an error
export const catchError(error, number) {
// First revise your data array to have the error in the right place
const updatedData = data
updatedData[number].error = error
// Now check to see if all your data objects are populated
// and update your loading state:
dispatch( checkAllData() )
return {
type: RECIEVE_ERROR,
data: updatedData,
}
}
export const checkAllData() {
// Check that every data object has something in it
if ( // fancy footwork to check each object in the data array and see if its empty or not
store.getState().data.every( dataSet =>
Object.entries(dataSet).length === 0 && dataSet.constructor === Object ) ) {
return {
type: STOP_LOADING
}
}
}
在旁边
如果您真的对API调用存在于每个孙代中的想法感到满意,但是整个孙网格在所有API调用完成之前都无法呈现,则您必须使用完全不同的解决方案。在这种情况下,您的孙子代必须从一开始就进行渲染,但要使用的css类display: none
,只有在将全局状态变量loading
标记为false 之后,该类才会更改。这也是可行的,但是除了React之外。