Javascript 获取后的条件呈现
如何在获取react native后进行条件渲染?我不想在获取后渲染组件 1.使用Javascript 获取后的条件呈现,javascript,reactjs,react-native,conditional-rendering,Javascript,Reactjs,React Native,Conditional Rendering,如何在获取react native后进行条件渲染?我不想在获取后渲染组件 1.使用响应数据成功获取 2.网络请求失败 3.如果服务器返回了空的responseData 现在,我只显示成功的结果。我还想显示失败的案例。在获取失败后,如何呈现Text组件。下面是我的代码 onPressSubmit() { fetch("xxxx", { method: "POST", headers: { Accept: "application/json",
响应数据成功获取
2.网络请求失败
3.如果服务器返回了空的responseData
现在,我只显示成功的结果。我还想显示失败的案例。在获取失败后,如何呈现Text
组件。下面是我的代码
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>
);
}
onPressSubmit(){
取回(“xxxx”{
方法:“张贴”,
标题:{
接受:“应用程序/json”,
“内容类型”:“应用程序/json”
},
正文:JSON.stringify({
url:this.state.url
})
})
.then((response)=>response.json())
.然后((响应数据)=>
{
这是我的国家({
res:responseData.message,
显示加载:false
},()=>console.log(this.state.res));
}).catch((错误)=>{
控制台错误(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;
返回(
Actions.addNewarticle({heading:title,authorname:author,description:text,htmlcon:html}}
>
{title}
);
}
render(){
返回(
{this.renderArticle()}
);
}
创建renderNetworkFailure
方法,在该方法中,您可以显示任何需要的内容。进入渲染
方法。检查您的响应数据是否可用?我有空白字符串
render(){
const isResponseData = (this.state.res == '') ? this.renderNetworkFailure() : this.renderArticle()
return(
<View>
{isResponseData}
</View>
);
render(){
const isResponseData=(this.state.res='')?this.renderNetworkFailure():this.renderArticle()
返回(
{isResponseData}
);
我想更准确地说,这就是你的答案,如果你想显示网络故障,那么你必须为它保留一个状态变量。(我也是react native的初学者,如果有任何错误,请更正)
render(){
返回(
{(this.state.err)?this.networkFailure():({this.state.res.length?this.renderArticle():this.renderNothing()})}
)
}
我能再给出一个条件吗,如果responseData
为空?为什么不能?这取决于您的要求。那会是什么情况?您能检查一下这个常量isResponseData=(this.state.res==“”)?this.renderNetworkFailure():this.renderArticle():this.rendermptydata()吗
No它是一个三元运算符。它类似于if-else。?
是if
和:
是else
render(){
return(
<View>
{(this.state.err)?this.networkFailure():({this.state.res.length? this.renderArticle():this.renderNothing()})}
</View>
)
}