setState后,render不执行



  • selectreview(rowID){

      var arrays = this.state.selectstate.concat();
      console.log('init arrays='+arrays);
      if(this.state.selectstate[rowID]==0){
        for(var i=0;i<arrays.length;i++){
    
          arrays[rowID]=1;
        }
        this.setState({
          selectstate:arrays,//我在这里执行了setState
        })
      console.log('点击收起 arrays='+arrays);
      console.log('点击收起 selectstate='+this.state.selectstate);
      }else{
        for(var i=0;i<arrays.length;i++){
    
          arrays[rowID]=0;
        }
        this.setState({
          selectstate:arrays,
        })
        console.log('点击展开 arrays='+arrays);
      }
    

    }
    renderRow(rowData: string, sectionID: number, rowID: number){
    console.log('selectstate='+this.state.selectstate);
    var ss=rowData.content;
    var content;
    var content2;
    if(ss.length>140){
    content=ss.substring(0,140);
    content2=ss;
    }else{
    content=ss;
    }
    //我在这里调用selectstate()方法,明明调用成功,状态却不改变这是怎么回事
    return(
    <View style={{width:deviceWidth,justifyContent:'center',alignItems:'center',flexDirection:'column'}}>

       <TouchableOpacity onPress={()=>this.selectreview(rowID)}>
       {this.state.selectstate[rowID]==0?
         <Text style={styles.textzhankai}>   {this.state.shouqitext}</Text>
         :<Text style={styles.textzhankai}>   {this.state.zhankaitext}</Text>
       }
       </TouchableOpacity>
       <View style={{width:350,height:1,backgroundColor:'#DDDDDD'}}></View>
      </View>
    );
    

    }



  • 你可以试试
    setState({
    prop: 'prop';
    }, () => {
    // 在这里做你setState后面的操作试试
    })



  • listview比较特殊,必须修改datasource才会触发更新。具体参考listview.datasource的文档。



  • @jesseying 这种写法我试过了,会报语法错误



  • @lh142332 jesseying的代码分号需要改成逗号



  • @chardlau 这个我知道,改成逗号了,它是最后一行语法报错


Log in to reply