在 React Native 中获取后的条件渲染
Conditional rendering after fetch in react native
react native fetch后怎么条件渲染?不想渲染组件
1.Successful 使用 responseData
获取
2.Network请求失败
3.If 从服务器
返回了空的 responseData
目前我只显示成功的result.I想显示失败的情况also.How我在不成功后渲染Text
组件fetch.Following是我的代码
onPressSubmit() {
fetch("xxxx", {
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "application/json"
},
body: JSON.stringify({
url: this.state.url
})
})
.then((response) => response.json())
.then((responseData) =>
{
this.setState({
res:responseData.message,
showLoading: false
}, () => console.log(this.state.res));
}).catch((error) => {
console.error(error);
});
}
renderArticle(){
const {title} = this.state.res;
const {image} = this.state.res;
const {text} = this.state.res;
const {id} = this.state.res;
const {author} =this.state.res;
const {html} = this.state.res;
return (
<TouchableOpacity
onPress={() => Actions.addNewarticle({heading:title, authorname:author, description:text, htmlcon:html})}
>
<CardSection>
<View style={{ flexDirection: "row" }}>
<Image
source={{ uri:image }}
style={styles.thumbnail_style}
/>
<Text style={styles.textStyle}>{title}</Text>
</View>
</CardSection>
</TouchableOpacity>
);
}
render(){
return(
<View>
{this.renderArticle()}
</View>
);
}
创建 renderNetworkFailure
方法,您可以在其中显示任何您想要的内容。进入render
方法。检查您的响应数据是否可用?我检查了空白字符串。
render(){
const isResponseData = (this.state.res == '') ? this.renderNetworkFailure() : this.renderArticle()
return(
<View>
{isResponseData}
</View>
);
我认为更准确地说,这就是你的答案的样子,如果你也想显示网络故障,那么你也必须为它保留一个状态变量。(我也是 react-native 的初学者如有不妥请指正)
render(){
return(
<View>
{(this.state.err)?this.networkFailure():({this.state.res.length? this.renderArticle():this.renderNothing()})}
</View>
)
}
react native fetch后怎么条件渲染?不想渲染组件
1.Successful 使用 responseData
获取
2.Network请求失败
3.If 从服务器
responseData
目前我只显示成功的result.I想显示失败的情况also.How我在不成功后渲染Text
组件fetch.Following是我的代码
onPressSubmit() {
fetch("xxxx", {
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "application/json"
},
body: JSON.stringify({
url: this.state.url
})
})
.then((response) => response.json())
.then((responseData) =>
{
this.setState({
res:responseData.message,
showLoading: false
}, () => console.log(this.state.res));
}).catch((error) => {
console.error(error);
});
}
renderArticle(){
const {title} = this.state.res;
const {image} = this.state.res;
const {text} = this.state.res;
const {id} = this.state.res;
const {author} =this.state.res;
const {html} = this.state.res;
return (
<TouchableOpacity
onPress={() => Actions.addNewarticle({heading:title, authorname:author, description:text, htmlcon:html})}
>
<CardSection>
<View style={{ flexDirection: "row" }}>
<Image
source={{ uri:image }}
style={styles.thumbnail_style}
/>
<Text style={styles.textStyle}>{title}</Text>
</View>
</CardSection>
</TouchableOpacity>
);
}
render(){
return(
<View>
{this.renderArticle()}
</View>
);
}
创建 renderNetworkFailure
方法,您可以在其中显示任何您想要的内容。进入render
方法。检查您的响应数据是否可用?我检查了空白字符串。
render(){
const isResponseData = (this.state.res == '') ? this.renderNetworkFailure() : this.renderArticle()
return(
<View>
{isResponseData}
</View>
);
我认为更准确地说,这就是你的答案的样子,如果你也想显示网络故障,那么你也必须为它保留一个状态变量。(我也是 react-native 的初学者如有不妥请指正)
render(){
return(
<View>
{(this.state.err)?this.networkFailure():({this.state.res.length? this.renderArticle():this.renderNothing()})}
</View>
)
}