我目前正在学习 React Native。我想写一个ListView。我正在遵循的教程使用不推荐使用的方法componentWillMount
,现在称为UNSAFE_componentWillMount
. 我用谷歌搜索了一个人说应该用componentDidMount
. 我的问题是当我将此方法添加到我的代码中时,应用程序会中断。
这是代码:
/* @flow */
import React, { Component } from "react";
import { ListView } from "react-native";
import { connect } from "react-redux";
import PropTypes from "prop-types";
import ListItem from "./ListItem";
class LibraryList extends Component {
componentDidMount = () => {
const ds = new ListView.DataSource({
rowHasChanged: (r1, r2) => r1 !== r2
});
this.dataSource = ds.cloneWithRows(this.props.libraries);
};
renderRow = library => <ListItem library={library} />;
render() {
return <ListView dataSource={this.dataSource} renderRow={this.renderRow} />;
}
}
LibraryList.propTypes = {
libraries: PropTypes.array
};
const mapStateToProps = state => {
return { libraries: state.libraries };
};
export default connect(mapStateToProps)(LibraryList);
这是我收到的错误消息TypeError: Cannot read property 'rowIdentities' of undefined
。我应该在这里使用哪种方法,或者我该如何解决这个问题?