你可以通过例如在请求完成之前延迟WebView的呈现:
constructor(props) {
super(props);
this.state = {
commonHtml = ''
};
}
componentDidMount() {
getMoviesFromApiAsync();
}
getMoviesFromApiAsync() {
fetch('*****some url*****')
.then((response) => response.json())
.then((responseJson) => {
// Assuming responseJson.data.nameA and responseJson.data.nameB exist
const { nameA, nameB } = responseJson.data;
this.setState({commonHtml: `my name is ${nameA} from ${nameB}`});
})
.catch((error) => {
console.error(error);
});
}
render() {
const commonHtml = { this.state };
return (
{commonHtml ?
:
(
Loading
)
}
);
}
此示例仅在this.state.commonHtml中存在某些内容时呈现WebView.
事实上,如果你不想做任何奇特的事情,甚至不需要三元.你可以干脆做
render() {
return (
);
}
当this.state.commonHtml发生更改时,setState会导致重新呈现.