React-Native - undefined 不是一个对象”(评估'this.state.*)_renderRow
React-Native - undefined is not an object" (evaluating 'this.state.*) _renderRow
我正在尝试调用 _renderRow() 函数内的状态,但我不断收到以下错误:
这是我的源代码:
源代码
var Store = require('./store/Store.js');
var MessageOptions = require('./MessageOptions.js')
var React = require('react');
var ReactNative = require('react-native');
var {
AppRegistry,
Image,
ListView,
StyleSheet,
Text,
View,
} = ReactNative;
var ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2})
class Application extends React.Component {
constructor(props) {
super(props);
this.state = {
selectedRowID: 20,
dataSource: ds
}
}
componentWillMount() {
this.getNewMessages();
}
getNewMessages() {
Store.getMessages().then((messages) => {
this.setState({
dataSource: this.state.dataSource.cloneWithRows(messages)
});
},(reason) => {
console.log("Error:", reason);
});
}
_renderRow(rowData: string, sectionID: number, rowID: number, highlightRow: (sectionID: number, rowID: number) => void) {
var currentSelectedRowID = this.state.selectedRowID;
return (
<View>
<View style={styles.row}>
<Image style={styles.thumb} source={require('../android/app/src/main/res/mipmap-hdpi/ic_launcher.png')} />
<Text style={styles.text}>
{rowData.text}
</Text>
</View>
<MessageOptions optionsData={rowData.options} message_uri={rowData.uri}/>
</View>
)
}
render() {
return (
<ListView
dataSource={this.state.dataSource}
renderRow={this._renderRow}
/>
)
}
};
var styles = StyleSheet.create({
row: {
flexDirection: 'row',
justifyContent: 'center',
padding: 10,
backgroundColor: 'white',
},
thumb: {
width: 64,
height: 64,
},
text: {
flex: 1,
},
news_item: {
paddingLeft: 10,
paddingRight: 10,
paddingTop: 15,
paddingBottom: 15,
marginBottom: 5
},
news_item_text: {
color: '#575757',
fontSize: 18
}
});
module.exports = Application;
错误来自我将 this.state.selectedRowID 存储到 var currentSelectedRowID 的 _renderRow 方法。
提前致谢。
解决方案
问题出在我的 ES6 class 构造函数上:
class Application extends React.Component {
constructor(props) {
super(props);
this._renderRow = this._renderRow.bind(this);
this.state = {
selectedRowID: 20,
dataSource: ds
}
}
解决方案是将以下行添加到我的构造函数中:
this._renderRow = this._renderRow.bind(this);
我正在尝试调用 _renderRow() 函数内的状态,但我不断收到以下错误:
这是我的源代码:
源代码
var Store = require('./store/Store.js');
var MessageOptions = require('./MessageOptions.js')
var React = require('react');
var ReactNative = require('react-native');
var {
AppRegistry,
Image,
ListView,
StyleSheet,
Text,
View,
} = ReactNative;
var ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2})
class Application extends React.Component {
constructor(props) {
super(props);
this.state = {
selectedRowID: 20,
dataSource: ds
}
}
componentWillMount() {
this.getNewMessages();
}
getNewMessages() {
Store.getMessages().then((messages) => {
this.setState({
dataSource: this.state.dataSource.cloneWithRows(messages)
});
},(reason) => {
console.log("Error:", reason);
});
}
_renderRow(rowData: string, sectionID: number, rowID: number, highlightRow: (sectionID: number, rowID: number) => void) {
var currentSelectedRowID = this.state.selectedRowID;
return (
<View>
<View style={styles.row}>
<Image style={styles.thumb} source={require('../android/app/src/main/res/mipmap-hdpi/ic_launcher.png')} />
<Text style={styles.text}>
{rowData.text}
</Text>
</View>
<MessageOptions optionsData={rowData.options} message_uri={rowData.uri}/>
</View>
)
}
render() {
return (
<ListView
dataSource={this.state.dataSource}
renderRow={this._renderRow}
/>
)
}
};
var styles = StyleSheet.create({
row: {
flexDirection: 'row',
justifyContent: 'center',
padding: 10,
backgroundColor: 'white',
},
thumb: {
width: 64,
height: 64,
},
text: {
flex: 1,
},
news_item: {
paddingLeft: 10,
paddingRight: 10,
paddingTop: 15,
paddingBottom: 15,
marginBottom: 5
},
news_item_text: {
color: '#575757',
fontSize: 18
}
});
module.exports = Application;
错误来自我将 this.state.selectedRowID 存储到 var currentSelectedRowID 的 _renderRow 方法。
提前致谢。
解决方案
问题出在我的 ES6 class 构造函数上:
class Application extends React.Component {
constructor(props) {
super(props);
this._renderRow = this._renderRow.bind(this);
this.state = {
selectedRowID: 20,
dataSource: ds
}
}
解决方案是将以下行添加到我的构造函数中:
this._renderRow = this._renderRow.bind(this);