【问题标题】:React Native SetTimeOut反应本机 SetTimeOut
【发布时间】:2015-10-21 16:38:53
【问题描述】:

我想在我的 RN 应用程序中使用计时器。我想在列表视图顶部显示waiting... 消息。这是场景,等待 2.5 秒以将新项目添加到列表,同时向用户显示信息消息,然后等待 1 秒以进行下一个循环并且不显示 waiting... 消息。我怎样才能做到这一点?我尝试了很多组合,但都没有按预期工作。

render(){
        return (
          <View style={styles.container}>
            <Text style={styles.waiting}>
              {'waiting...'}
            </Text>
            <ListView
              dataSource={this.state.dataSource}
              renderRow={this.renderCell}
              automaticallyAdjustContentInsets={false}
              style={styles.listView}
              />
          </View>
        );
      }

【问题讨论】:

  • OP 应该对答案给予肯定。我相信答案还不错,以至于他们投了反对票。

标签: javascript reactjs settimeout react-native


【解决方案1】:

如果我理解正确,试试这个代码。希望对您有所帮助...

getInitialState: function() {
   return {
     showLoading : true,//To show 'Waiting Text in initial state.
   };
},

componentDidMount() {
   this.setTimeout( () => {
      this._hideLoadingView();//To hide 'Waiting' Text after 1 second
   },1000);
},

_hideLoadingView() {
   this.setState({
     showLoading: false, //set state to hide 'Waiting'
   });
},


render(){
    return (
      <View style={styles.container}>
        {this._loadingTextView()}
        <ListView
          dataSource={this.state.dataSource}
          renderRow={this.renderCell}
          automaticallyAdjustContentInsets={false}
          style={styles.listView}
          />
      </View>
    );
  },

_loadingTextView(){
  if(this.state.showLoading){//To show or hide 'Waiting'
  return( 
     <Text style={styles.waiting}>
     Waiting.....
     </Text>
   )
  }
}

【讨论】:

    【解决方案2】:

    Pyo 几乎是对的。唯一的问题是它只会发生一次。我不会在两者之间循环。以下可能会成功

    getInitialState: function() {
       return {
         showLoading : true,//To show 'Waiting Text in initial state.
       };
    },
    
     componentWillMount() {
      this._showLoadingView; // start by displaying the wait
    },
    
    _hideLoadingView() {
       this.setTimeout(this._showLoadingView,1000);
       this.setState({
         showLoading: false, //set state to hide 'Waiting'
       });
    },
    _showLoadingView(){
       // Do other fetch instructions here
       this.setTimeout(this._hideLoadingView,2500);
       this.setState({
         showLoading: true,
       });
    },
    render(){
        return (
          <View style={styles.container}>
            {this.textRender()}
            <ListView
             dataSource={this.state.dataSource}
             renderRow={this.renderCell}
             automaticallyAdjustContentInsets={false}
             style={styles.listView}
             />
          </View>
        );
      },
    
    textRender(){
      if(this.state.showLoading){//To show or hide 'Waiting'
          return( 
             <Text style={styles.waiting}>
                 Waiting.....
             </Text>
           )
      }
      else{
          return <View />
      }
    
    }
    

    如果你想使用 this.setTimeout(推荐)记得使用 TimerMixin 否则使用 setTimeout

    【讨论】:

    • 好的,但我不想返回新视图,Text 和 ListView 都应该是同一个视图。我想更改 this.setState({waiting: 'waiting...'});this.setState({waiting: ''}); 之类的内容
    • 在这种情况下,文本将随着列表视图的增长而滚动。这将确保等待像标题一样位于顶部。同样返回一个空视图也不会影响体验
    • 文本不在列表视图中。
    • 你也可以看看listview的headerView。
    • 如果您返回空文本而不是空视图,则 textView 将保留其样式,并带有空文本。我猜你没想到会这样
    猜你喜欢
    • 2020-10-07
    • 2023-03-16
    • 1970-01-01
    • 2020-08-10
    • 2016-04-18
    • 2021-12-26
    • 2019-05-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多