1

因为一些性能问题,我希望只刷新改变行,但是虽然rowHasChanged返回true,但行没有更新或执行renderRow。热重载后,更新只有一次成功,我很困惑。

index.ios.js

  import {AppRegistry} from 'react-native';
  import React from 'react';
  import ImmutableDataSource from 'react-native-immutable-listview-datasource';
  import {View,Text,ListView,StyleSheet} from 'react-native';
  import Immutable from 'immutable';

  const ds = new ImmutableDataSource();
  console.log(ds);
  let styles = StyleSheet.create({
    selected:{
      color:"#FD973C"
    },
    unselected:{

    }
  })

  class List extends React.Component {
    constructor(props){
      console.log('constructor');
      super(props);
      this.state = {
        data : Immutable.fromJS([
          {title:'row111111'},
          {title:'row222222'},
          {title:'row333333'},
          {title:'row444444'},
          {title:'row555555'},
        ])
      }
    }
    handlePress = (rowData,rowID)=>{
      console.log('handlePress',rowID);
      this.setState({
        data:this.state.data.setIn([rowID,'selected'],!this.state.data.getIn([rowID,'selected']))
      })
    }
    renderRow = (rowData,sectionID,rowID) => {
      console.log('render row',rowData.get('title'),rowID);

      let style;
      if(rowData.get('selected')){
        style = styles.selected;
      }else{
        style = styles.unselected;
      }
      return <Text
          style={style}
          onPress={this.handlePress.bind(this,rowData,rowID)}
        >{rowData.get('title')}</Text>;
    }

    render() {
      return (
        <View>
        <ListView
          enableEmptySections={true}
          initialListSize={10}
          pageSize={20}
          onEndReachedThreshold={0}
          ref={this.props.scrollRef}
          dataSource={ds.cloneWithRows(this.state.data)}
          renderRow={this.renderRow}
        />
        </View>
      );
    }

  }

  AppRegistry.registerComponent('NovelReader', ()=>{
    return List;
  });

热加载前,点击item,不更新点击行,

热加载后,点击项目,重新渲染点击线

4

0 回答 0