【发布时间】:2018-09-02 08:27:45
【问题描述】:
免责声明:我是 RN 新手。 我已经测试了其他 similar questions 的多个解决方案,但到目前为止都没有成功。
我有一个像这样渲染两个孩子的父母
export default class ParentComponent extends Component {
constructor(props) {
super(props)
}
render() {
return (
<View>
<Foo name="a" ref={foo => {this.foo = foo}} {...this.props} />
<Text>-----------</Text>
<Foo name="b" />
<Text>-----------</Text>
<Button
onPress={this.foo.myFunction()}
title="Start"
color="#841584"
/>
</View>
)
}
}
我的类 Foo 里面有一个函数可以启动一些进程:
class Foo extends Component {
myFunction(){
// Some stuff here
}
}
当我按下按钮时,如何为我的孩子调用这个myFunction?或者,是否可以仅使用一个 onPress 为两个 Child 调用该函数并避免为每个 Child 创建两个 Button ?
【问题讨论】:
-
为什么还要在这里使用孩子?在我看来,您要么不需要孩子,要么可以封装所有 Foo 逻辑及其组件,这对您来说不是问题。如果你决定你需要这个拱门,那么这是一个骗子:stackoverflow.com/questions/26176519/reactjs-call-parent-method
-
我测试了这个问题中提供的解决方案,但它不起作用。这是我在这里提出的解决方案之一
标签: javascript reactjs react-native components