React native 在超时功能中显示视图内容几秒钟

React native 在超时功能中显示视图内容几秒钟,react-native,React Native,我需要在几秒钟内显示响应或错误消息,如何使用setTimeout函数或其他方式执行此操作。感谢您的帮助 <View> {!!this.state.error && ( <Text style={styles.message}> {this.state.errorMessage} </Text> )} {!!this.state.response && (

我需要在几秒钟内显示响应或错误消息,如何使用setTimeout函数或其他方式执行此操作。感谢您的帮助

<View>
    {!!this.state.error && (
        <Text style={styles.message}>
          {this.state.errorMessage}
        </Text>
    )}
    {!!this.state.response && (
        <Text style={styles.successMessage}>
          {this.state.response}
        </Text>
    )}
</View>

{!!this.state.error&&(
{this.state.errorMessage}
)}
{!!this.state.response&&(
{this.state.response}
)}

此视图显示来自API的响应消息或错误消息。

当您要触发错误或响应时

this.setState({error: true},
  () => this.setTimeout(
     this.setState({error: false}), 5000
  )
)

                 OR

this.setState({error: true});
this.setTimeout(this.setState({error: false}), 5000);

这是写在渲染函数中的。我很感谢您的回复,但因为我是新手,所以我会以本地方式进行响应。。我应该在哪里写这个函数。提前感谢。我需要根据某些情况显示这两个选项中的任何一个,持续5秒钟