gpt4 book ai didi

javascript - React Native ListView 到 FlatList 迁移

转载 作者:行者123 更新时间:2023-11-30 11:12:58 25 4
gpt4 key购买 nike

所以我开始从视频中学习 react-native,他们使用了 ListView,但由于 ListView 很快就会被弃用,并将被删除。我知道 FlatList 将是合适的替代品,但作为初学者我无法迁移到 Flatlist。

import React, { Component } from "react";
import { ListView } from 'react-native';
import { connect } from 'react-redux';
import ListItem from './ListItem';

class LibraryList extends Component {

componentWillMount() {
const ds = new ListView.DataSource({
rowHasChanged: (r1,r2) => r1 !==r2
});

this.dataSource =ds.cloneWithRows(this.props.libraries);
}

renderRow(library) {
return <ListItem library = { library } />;
}
render() {
return(
<ListView
dataSource = {this.dataSource}
renderRow = {this.renderRow}
/>

);
}
}

const mapStateToProps = state => {

return { libraries: state.libraries };

};

export default connect(mapStateToProps) (LibraryList);

最佳答案

欢迎使用 stackoverflow。

迁移应该非常简单,您不再需要 dataSource。您可以将项目数组直接传递给组件。

import React, { Component } from "react";
import { FlatList } from 'react-native';
import { connect } from 'react-redux';
import ListItem from './ListItem';

class LibraryList extends Component {
renderRow({item}) {
return <ListItem library = { item } />;
}
render() {
return(
<FlatList
data = {this.props.libraries}
renderItem = {this.renderRow}
/>
);
}
}

const mapStateToProps = state => {

return { libraries: state.libraries };

};

export default connect(mapStateToProps) (LibraryList);

文档标题 here了解更多信息。

关于javascript - React Native ListView 到 FlatList 迁移,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52905081/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com