React Native ListView不会通过单击重新呈现状态更改

时间:2016-07-07 12:09:41

标签: listview reactjs onclick react-native react-native-listview

以下是我的示例代码,我希望点击时更新ListView个元素。

此行定义样式:

(this.state.selectedField.id==field.id)?'green':'white'

在此示例中,活动View应以绿色突出显示。状态在handleClick()内更新,但未调用renderField()方法。

如何通过点击触发状态更改,使ListView重新渲染?

RNPlayNative Link

import React, {Component} from 'react';
import {
  AppRegistry,
  View,
  ListView,
  Text,
  TouchableOpacity
} from 'react-native';

class SampleApp extends Component {

  constructor(props) {
    super(props);
    var ds = new ListView.DataSource({
        rowHasChanged: (row1, row2) => row1 !== row2,
    });
    this.state = {
      fields: ds.cloneWithRows([
        {id:0},{id:1},{id:2}
      ]),
      selectedField: {id:0}
    };
  }

  handleClick(field) {
    console.log("Selected field:",field);
    this.setState({
      selectedField: field
    });
  }

  renderField(field) {
    return (
      <TouchableOpacity onPress={this.handleClick.bind(this, field)} >
        <View style={{backgroundColor:(this.state.selectedField.id==field.id)?'green':'white'}}>
          <Text style={{left:0, right:0, paddingVertical:50,borderWidth:1}}>{field.id}</Text>
        </View>
      </TouchableOpacity>
    );
  }

  render() {
    return (
      <View>
        <ListView
          dataSource={this.state.fields}
          renderRow={this.renderField.bind(this)}
        />
      </View>
    );
  }
}

AppRegistry.registerComponent('SampleApp', () => SampleApp);

3 个答案:

答案 0 :(得分:3)

当dataSource更改时,列表会再次呈现,并且在您的示例中,因为dataSource永远不会更改,所以listview永远不会重新呈现。这可以通过在状态变量中添加一个字段来保存dataSource的数据来实现。在componentDidMount方法中,让dataSource克隆行包含数据状态变量。每当您想要重新渲染列表视图时,您只需要更改数据状态变量,列表将自动重新渲染。我已经更改了数据,以便为每个对象添加选定的状态。这是更新的代码。

class SampleApp extends Component {

  constructor(props) {
     super(props);
     var ds = new ListView.DataSource({
         rowHasChanged: (row1, row2) => row1 !== row2,
    });
     var dataVar = [
         {
           id:0,
           selected: true,
         },{
           id:1,
           selected: false,
         },{
           id:2,
           selected: false,
         }
       ];
     this.state = {
       data: dataVar,
       fields: ds,
     };
   }

   componentDidMount() {

     this.setState({
       fields: this.state.fields.cloneWithRows(dataVar)
     });
   }


   handleClick(field) {
     console.log(field);
     field.selected = !field.selected;

     var dataClone = this.state.data;
     console.log(dataClone);

     dataClone[field.id] = field;

     this.setState({
       data: dataClone,
     });
   }

   renderField(field) {
     let color = (field.selected == true)?'green':'white';
     return (
       <TouchableOpacity onPress={this.handleClick.bind(this, field)} >
         <View style={{backgroundColor:color}}>
           <Text style={{left:0, right:0, paddingVertical:50,borderWidth:1}}>     {field.id}</Text>
         </View>
       </TouchableOpacity>
     );
   }

   render() {
     return (
       <View>
         <ListView
           dataSource={this.state.fields}
           renderRow={(field) => this.renderField(field)}
         />
       </View>
     );
   }
 }

这是一个有效的工作sample

答案 1 :(得分:1)

已经多次回答,如果你想更改道具上的行渲染,如果你想要排版更新,这个道具需要输入你的数据。

见这2个答案:

答案 2 :(得分:0)

这是我对问题的解决方案,但处理每次点击仍然需要很长时间:

handleClick(field) {
  newFields = this.state.fields.map( (x) => x.key === field.key && x.active !== true ? {...x,active:true} : {...x,active:false})  
  this.setState({
    fields:newFields,
    dataSource: this.state.dataSource.cloneWithRows(newFields)
  });
}