简体   繁体   English

将本机设置状态反应为 true 一秒钟

[英]React native set state to true for one second

I have a state:我有一个状态:

state = {
showView: false,
}

How can I make a function that when called it changes the showView state to true for 1 second then change it back to false?如何创建一个函数,当调用它时将showView状态更改为true 1 秒,然后将其更改回 false?

this.setState({
  showView: true,
}, () => setTimeout(() => {
  this.setState({
    showView: false,
  })
}, 1000))

This will set the state back to false after briefly (1 second) set it to true, using the React setState callback function.这将在使用 React setState 回调函数短暂(1 秒)将状态设置为 true 后将状态设置回 false。

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

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