reactjs - React Native ListView 性能太慢?

标签 reactjs reactjs-flux react-native react-native-listview

我的应用程序中有 113 条记录。react native listView 需要 3 秒多的时间来渲染所有行。我怎样才能使其高效,以便最大限度地减少时间消耗并使应用程序体验流畅。我已经在 ios 中检查了相同的应用程序,与 react native 应用程序版本相比,它的效率更高。我的每一行都有一个头像图像、名称和按钮。

这是我的 ListView 代码

var Ziglist = React.createClass({
getInitialState: function() {
     return {
        isLoading: true,
        resultsData: new ListView.DataSource({
        rowHasChanged: (row1, row2) => row1 != row2 }),
      };
},

componentWillMount: function(){
  PeopleStore.listen(this.onChange);
  PeopleActions.getPeopleFollowing();
},

componentWillUnmount() {
  PeopleStore.unlisten(this.onChange);
},

onChange(state) {
this.setState({resultsData: this.getDataSource(state.resultsData)})
},

getDataSource: function(mediaItem: Array<any>): ListView.dataSource {
  return this.state.resultsData.cloneWithRows(mediaItem)
},
render: function() {
    return (
  <View style={{flex: 1}} >
  <ListView
    dataSource={this.state.resultsData}
    renderRow={this.renderRow}
    renderSeperator={this.renderSeperator}
    contentInset={{top: 0}}
    automaticallyAdjustContentInsets={false}
  />
  <Button
    containerStyle={styles.goAhead}
    style={styles.base}
    onPress={() => this.onStartPress()}>
    <Text style={[styles.base,styles.go]}>Ok, Lets GO > </Text>
  </Button>
 </View>
);
},
renderSeparator: function (sectionID: number | 
string, rowID: number | string, adjacentRowHighlighted: boolean ) {
 return (
    <View key={"SEP_" + sectionID + "_" + rowID} />
 );
},
renderRow: function (
 media: Object,
 sectionID: number | string,
 rowID: number | string,
 highlightRowFunction: (sectionID: ?number | string, 
 rowID: ?number | string) => void,) {
  return (
    <CelebrityRow
     media={media}
     onSelect={() => this.selectMediaItem(media)}
     onHighlight={() => highlightRowFunction(sectionID,rowID)}
     onDeHighlight={() => highlightRowFunction(null,null)}
    />
 );
},
selectMediaItem: function (mediaItem) {
this.props.navigator.push({
  name: 'celebrityDetailView',
  passProps: {
    mediaItem: mediaItem
  }
});
}

这是名人行的代码

var CelebrityRow = React.createClass({
getInitialState: function() {
    return {
        following_ids: FollowingStore.getState().following_ids,
        unfollowing_ids: FollowingStore.getState().unfollowing_ids,
        icon: null,
        follow: true
    }
},
componentWillMount: function() {
    if (_.indexOf(this.state.unfollowing_ids, this.props.media.id) > -1) {
        this.setState({
            follow: !this.state.follow
        });
    }
},
componentWillUnmount: function() {},
componentDidMount: function() {
    var _unfollowing_ids = FollowingStore.getState().unfollowing_ids;

    if (_unfollowing_ids.length > 0 && this.state.follow === false) {
        var following_arr = PeopleStore.getState().resultsData;
        var _following_ids = FollowingStore.getState().following_ids;

        _.each(_unfollowing_ids, function(id) {
            var find = _.findWhere(following_arr, {
                id: id
            });
            following_arr = _.without(following_arr, find);
        });

        var following_ids = _.difference(_following_ids, _unfollowing_ids);

        this.setState({
            unfollowing_ids: [],
            following_ids: following_ids
        });
        var _this = this;
        setTimeout(function() {
            FollowingActions.updateFollowingIdsWithStorage(following_ids);
            FollowingActions.updateUnFollowingIds([]);
            PeopleActions.updatePeopleFollowingWithStorage(following_arr);
            _this.setState({
                follow: true
            })
        }, 1000);

    }

},
onChange: function(state) {
    // this.setState({unfollowing_ids: state.unfollowing_ids});
},
onAddPress: function(media) {

    this.setState({
        follow: !this.state.follow
    });
    FollowingActions.updateUnFollowingIds(media.id);
},
render: function() {

    return ( <
        View style = {
            styles.row
        } >
        <
        TouchableHighlight onPress = {
            this.props.onSelect
        }
        onShowUnderlay = {
            this.props.onHighlight
        }
        onHideUnderlay = {
            this.props.onDeHighlight
        } >
        <
        View style = {
            styles.cellContainer
        } >
        <
        Image source = {
            {
                uri: this.props.media.avatar_url
            }
        }
        style = {
            styles.cellImage
        }
        /> <
        Text style = {
            styles.CelebrityName
        }
        numberOfLines = {
            1
        } > {
            this.props.media.name
        } < /Text>

        <
        View style = {
            styles.celebrityAdd
        } >
        <
        Button onPress = {
            () => this.onAddPress(this.props.media)
        } > {
            (this.state.follow ?
                ( <
                    Image source = {
                        require("../assets/tick-mark.png")
                    }
                    style = {
                        styles.addIcon
                    }
                    />
                ) : ( <
                    Image source = {
                        require("../assets/img-small-add.png")
                    }
                    style = {
                        styles.removeIcon
                    }
                    />
                )
            )
        } <
        /Button> <
        /View>

        <
        /View> <
        /TouchableHighlight> <
        /View>

    );
}

});

最佳答案

您可以设置initialListSize属性来提高渲染性能,而不是一次渲染全部100多行。

引用:http://facebook.github.io/react-native/releases/0.30/docs/performance.html#listview-initial-rendering-is-too-slow-or-scroll-performance-is-bad-for-large-lists

关于reactjs - React Native ListView 性能太慢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38692466/

相关文章:

javascript - 如何在 Material-UI Circular Progress 中添加额外的描边

javascript - 在页面中渲染带有 HTML 标签的文本

browserify - Flux 架构循环依赖

javascript - react View 未成功收听商店

utf-8 - react native : How to decode base64 encoded string?

ios - Expo 应用程序因react-native-google-mobile-ads 崩溃 [app.json 中的 react-native-google-mobile-ads 键中未找到 ios_appd_id 键]

javascript - react : Implement Login with BASIC Authentication

reactjs - 无法使用 create-react-app 创建应用程序

navigation - React-native 导航实验示例?

node.js - 生成yarnpkg ENOENT