Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/react-native/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
React native 反应本族语:can';t从子方法调用父方法_React Native - Fatal编程技术网

React native 反应本族语:can';t从子方法调用父方法

React native 反应本族语:can';t从子方法调用父方法,react-native,React Native,我在父组件中有这个函数,我试图在子组件中调用这个方法/函数,如下所示: buttonPressed = () => { console.log('button pressed') } <childComp ref={ref => this.feedback = ref} onPress={this.buttonPressed} /> 按钮按下=()=>{ console.log('按钮按下') } this.feedback=ref} onP

我在父组件中有这个函数,我试图在子组件中调用这个方法/函数,如下所示:

buttonPressed = () => {
  console.log('button pressed')
}

<childComp ref={ref => this.feedback = ref}
           onPress={this.buttonPressed}
/>
按钮按下=()=>{
console.log('按钮按下')
}
this.feedback=ref}
onPress={this.buttonPressed}
/>
儿童:

<Animated.View>
  ...   // other views
  <View>
    <TouchableOpacity onPress={this.props.buttonPressed}> // calling here
     ...
    </TouchableOpacity>

  </View>
</Animated.View>

...   // 其他观点
//在这里打电话
...

不管我怎么做,它都不起作用。

您正在将Press-prop传递给child,并调用child中的按钮Pressed。应该是这样的:


...   // 其他观点
//在这里打电话
...

我认为在子组件中,您应该调用
this.props.onPress()
,因为您要传递的属性的名称是
onPress
。另外,当您想要调用函数时,不要忘记放置
()
。因此,它应该是:

<Animated.View>
  ...   // other views
  <View>
    <TouchableOpacity onPress={this.props.onPress()}> // calling here
     ...
    </TouchableOpacity>

  </View>
</Animated.View>

...   // 其他观点
//在这里打电话
...