简体   繁体   English

在获取本机反应后进行条件渲染

[英]Conditional rendering after fetch in react native

How to do conditional rendering after fetch in react native?I wan't to render components after 1.Successful fetch with the responseData 2.Network request fails 3.If there is empty responseData returned from server 怎么办条件呈现取指令后发生反应,本土?我wan't渲染组件1.Successful与取后responseData 2.Network请求失败3,如果没有空responseData从服务器返回

For now I'm displaying only the successful result.I want to display the failure case also.How do I render Text component after unsuccessful fetch.Following is my code 现在我只显示成功的结果,我也想显示失败的情况,获取失败后如何渲染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>
);
}

Create renderNetworkFailure method in which you can display whatever you want. 创建renderNetworkFailure方法,您可以在其中显示所需的任何内容。 Into render method. 进入render方法。 check your response data is available or not? 检查您的响应数据是否可用? I have check blank string. 我检查了空白字符串。

render(){
const isResponseData = (this.state.res == '') ? this.renderNetworkFailure() : this.renderArticle()
return(
 <View>
{isResponseData}
 </View>
);

I think more precisely this is how your answer would look like, and if you want to show for the network failure as well then you must keep a state variable for it also.(Im also a beginner in react-native please correct if there is anything wrong) 我想更确切地说,这就是您的答案的样子,如果还要显示网络故障,则还必须为其保留状态变量。(我也是react-native的初学者,如果有的话哪里不对了)

render(){
return(
<View>
{(this.state.err)?this.networkFailure():({this.state.res.length? this.renderArticle():this.renderNothing()})}
</View>
)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM