【问题标题】:binding a function passed to a component绑定传递给组件的函数
【发布时间】: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


【解决方案1】:

无需大量修改代码即可解决此问题的方法是添加 this.renderRow = this.renderRow.bind(this) 给你的类构造函数。

class New extends Component{   
  constructor(){
    this.renderRow = this.renderRow.bind(this)
  }

  render(){...} 
}

你已经添加了属性renderRow = { this.renderRow },实际上renderRow是用binded to null object执行的。尝试在renderRow中控制台this你会发现它是GlobalObject而不是你想要的Class MovieList。

【讨论】:

  • 我可能会感到困惑,但这真的是解决这个问题的正确方法吗?答案并没有首先解释是什么导致了这个问题,而且看起来像是一个黑客攻击。
  • @PixelCakeGames 感谢您发现一开始没有给出答案,虽然我不知道这是一个“黑客”而不是解决方案。很明显,renderRow 方法执行宽度绑定到一个`null`对象,这就是@Leahcim崩溃的原因。
【解决方案2】:

尝试使用 es6 语法:

selectMovie = (movie) => {
  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,
    });
  }
}

然后

constructor(props) {
  super(props);

  this.selectMovie = this.selectMovie();
  this.state = {
    dataSource: new ListView.DataSource({
      rowHasChanged: (row1, row2) => row1 !== row2,
    })
  };
}

和

renderRow = (...) => {
  return (
    <ListCell 
      onSelect={this.selectMovie(movie)}
      onHighlight={() => highlightRowFunc(sectionID, rowID)}
      onUnhighlight={() => highlightRowFunc(null, null)}
      movie={movie}
    />
  );
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-03
    • 1970-01-01
    • 2018-07-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多