javascript - 无法在 native react 中调用函数

标签 javascript reactjs react-native

我收到一条错误消息,提示 this2.sampleFunction is not an object ,我已尝试添加一个构造函数以及之前解决方案中的所有内容。

export default class Play extends React.Component {

  sampleFunction() {
    console.log('Hello'); 
  }

  anotherFunction() {
    return (
      <Button
        onPress={() => this.sampleFunction.bind(this)} />
    );
  }

  render() {
    <Deck
      anotherFunction={this.anotherFunction()}
    />
  }
}

编辑:这是甲板组件中的代码,它主要是一个 View 标签

  render() {
return (
  <View>
    {this.props.anotherFunction()}
  </View>
);
}

这是错误的图像:error

最佳答案

我遇到了这个问题,它和我在评论中描述的一样。您正在将一个元素从 Play 传递到 Deck,但 Deck 需要一个函数。请将 Play 的渲染方法更改为

render() {
    return (<Deck
      anotherFunction={this.anotherFunction.bind(this)}
    />);
}

关于javascript - 无法在 native react 中调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48814457/

相关文章:

javascript - 函数体的解结构变量和变量部分之间的区别

html - 我的 webpack 没有生成我的 css 文件

react-native - 如何在 createBottomTabNavigator 中获取背景图片?

javascript - 在 native react 中,如何将base64图像转换为jpg然后保存到临时路径?

javascript - 通过扩展运算符中的变量传递对象属性

typescript - 如何通过 Typescript for Flow 导入 React-Native 类型?

javascript - 针对未定义的 this.props.navigation.navigate 使用react导航器问题

javascript - 不使用 .pipe() 调用 gulp-notify

特定数字范围之间的 Javascript Math.floor 问题

javascript - 如何解析 bbcode 正则表达式?