データを取得するのに理想的な場所はcomponentWillMount
ですが、データが取得される前に子コンポーネントがレンダリングされる可能性があるため、非同期性があるため2つのことができます。
のように、結果がフェッチされるまで、コンポーネントをレンダリングしていない負荷状態を維持する:その他の方法は、空の小道具を持っているし、子コンポーネントにチェックするために実行することです
constructor() {
super();
this.state = {
isLoading: true,
// other states
}
}
componentWillMount() {
//your async request here
}
render() {
if(this.state.isLoading) {
return null; // or you can render laoding spinner here
} else {
return (
//JSX here with the props
)
}
}
:
私は個人的に好む
constructor() {
super();
this.state = {
someProps: null;
}
}
componentWillMount() {
//your async request here
}
render() {
return (
<Child someProps={this.state.someProps}/>
)
}
子供
render() {
if(this.props.someProps == null)
return null;
else {
return (//JSX contents here);
}
}
最初の方法 –
ありがとう!これはとても役に立ちました!それは働いた:) – code83
何らかの理由で、私のユニットテストは今でも失敗している。クラッシュすることなくオブジェクトをレンダリングするための基本的なテストは失敗します。上記の解決策とはどのように関連していますか?それらを修正するにはどうすればよいですか? – code83