【发布时间】:2016-02-17 01:48:54
【问题描述】:
阅读this SO answer,我明白了当我将一个函数传递给一个react组件时,我必须像这样在构造函数中绑定一个函数
constructor(props) {
super(props);
//binding function
this.renderRow = this.renderRow.bind(this);
this.callThisFunction = this.callThisFunction.bind(this);
}
否则我会收到这样的错误。
null 不是对象:评估
this4.functionName
按照该建议,我在构造函数中绑定了函数,但仍然遇到同样的错误。
我正在根据 react native repo 中的 Movies 示例使用 React Native 制作 Master/Detail 应用程序,但我不使用此语法
var SearchScreen = React.createClass({
(这就是 repo),而是这种 ES6 风格的语法
class ListOfLists extends Component {
在我的列表视图中,我呈现这样的一行。
class MovieList extends Component{
constructor(props){
super(props);
this.selectMovie = this.selectMovie.bind(this);
this.state = {
dataSource: new ListView.DataSource({
rowHasChanged: (row1, row2) => row1 !== row2,
}),
};
}
renderRow(
movie: Object,
sectionID: number | string,
rowID: number | string,
highlightRowFunc: (sectionID: ?number | string, rowID: ?number | string) => void,
) {
console.log(movie, "in render row", sectionID, rowID);
return (
<ListCell
onSelect={() => this.selectMovie(movie)}
onHighlight={() => highlightRowFunc(sectionID, rowID)}
onUnhighlight={() => highlightRowFunc(null, null)}
movie={movie}
/>
);
}
selectMovie(movie: Object) {
if (Platform.OS === 'ios') {
this.props.navigator.push({
title: movie.name,
component: TodoListScreen,
passProps: {movie},
});
} else {
dismissKeyboard();
this.props.navigator.push({
title: movie.title,
name: 'movie',
movie: movie,
});
}
}
render(){
var content = this.state.dataSource.getRowCount() === 0 ?
<NoMovies /> :
<ListView
ref="listview"
renderSeparator={this.renderSeparator}
dataSource={this.state.dataSource}
renderFooter={this.renderFooter}
renderRow={this.renderRow}
automaticallyAdjustContentInsets={false}
keyboardDismissMode="on-drag"
keyboardShouldPersistTaps={true}
showsVerticalScrollIndicator={false}
renderRow={this.renderRow}
}
关键行是this.selectMovie(movie)。当我单击带有电影名称的行时,出现错误
null 不是对象:
evaluating this4.selectMovie
问题:为什么它告诉我null is not an object 或者更确切地说为什么该函数为空?
更新:
我在代码中添加了渲染方法以显示renderRow 的使用位置
【问题讨论】:
-
我看不到你在哪里使用
renderRow,但你可能也需要绑定它。但是,您不需要结合箭头函数和绑定。 -
@sodik 我更新了 OP 以显示
renderRow的使用位置
标签: react-native