我正在开发我的第一个React Native应用程序。我想要实现的是从父组件执行子函数,这是情况:
儿童
export default class Child extends Component {
...
myfunct: function() {
console.log('Managed!');
}
...
render(){
return(
<Listview
...
/>
);
}
}
父
export default class Parent extends Component {
...
execChildFunct: function() {
...
//launch child function "myfunct"
...
//do other stuff
}
render(){
return(
<View>
<Button onPress={this.execChildFunct} />
<Child {...this.props} />
</View>);
}
}
在这个例子中,当我按下父类中的按钮时,我想记录'Managed!'
。怎么可行?
答案 0 :(得分:21)
您可以为子组件添加引用:
<Child ref='child' {...this.props} />
然后像这样调用孩子的方法:
<Button onPress={this.refs.child.myfunc} />
答案 1 :(得分:17)
Nader Dabit的答案已经过时,因为在ref属性has been deprecated中使用了字符串文字。这是我们在2017年9月的表现:
<Child ref={child => {this.child = child}} {...this.props} />
<Button onPress={this.child.myfunc} />
功能相同,但我们不是使用String来引用组件,而是将其存储在全局变量中。
答案 2 :(得分:3)
我认为你误解了组件结构。
假设您的孩子是为您的其他组件生成按钮的组件。在此层次结构中,您的孩子必须通知其父母已被按下。
孩子-----&gt;父
export default class Child extends Component {
return(
<Button onPress={this.props.onPress } />
);
}
在您的父组件中,使用子组件为您生成一个按钮。通过这种方式,您可以将子组件作为独立按钮使用任何其他组件。
export default class Parent extends Component {
constructor(props) {
super(props);
this.execChildFunct=this.execChildFunct.bind(this)
}
execChildFunct: function() {
console.log('Managed!');
}
return (
<Child onPress = {this.execChildFunct}></Child>
)
}
答案 3 :(得分:3)
它正在反应中。我希望它可以帮到你。
class Child extends React.Component {
componentDidMount() {
this.props.onRef(this)
}
componentWillUnmount() {
this.props.onRef(null)
}
method() {
console.log('do stuff')
}
render() {
return <h1>Hello World!</h1>
}
}
class EnhancedChild extends React.Component {
render() {
return <Child {...this.props} />
}
}
class Parent extends React.Component {
onClick = () => {
this.child.method() // do stuff
};
render() {
return (
<div>
<EnhancedChild onRef={ref => (this.child = ref)} />
<button onClick={this.onClick}>Child.method()</button>
</div>
);
}
}
ReactDOM.render(<Parent />, document.getElementById('root'))
原始解决方案:
答案 4 :(得分:0)
父母的简单方法->子函数调用
/* Parent.js */
import React, { Component } from "react";
import { TouchableOpacity, Text } from "react-native";
import Child from "./Child";
class Parent extends React.Component {
onChildClick = () => {
this.child.childFunction(); // do stuff
};
render() {
return (
<div>
<Child onRef={(ref) => (this.child = ref)} />
<TouchableOpacity onClick={this.onChildClick}>
<Text>Child</Text>
</TouchableOpacity>
</div>
);
}
}
/* Child.js */
import React, { Component } from "react";
class Child extends React.Component {
componentDidMount() {
this.props.onRef(this);
}
componentWillUnmount() {
this.props.onRef(undefined);
}
childFunction() {
// do stuff
alert("childFunction called");
}
render() {
return <View>Hello World!</View>;
}
}
原始解决方案: https://github.com/kriasoft/react-starter-kit/issues/909
答案 5 :(得分:0)
以下是使用功能组件实现此目的的方法:
父母
useRef()
在父组件中为子组件提供一个引用:const childRef = useRef()
// ...
return (
<ChildComponent ref={childRef} />
)
...
儿童
ref
作为构造函数参数之一传递:const ChildComponent = (props, ref) => {
// ...
}
useImperativeHandle
库导入 forwardRef
和 'react'
方法:import React, { useImperativeHandle, forwardRef } from 'react'
useImperativeHandle
将函数绑定到 ref
对象,这将使父级可以访问这些函数这些方法在内部不可用,因此您可能需要使用它们来调用内部方法。
const ChildComponent = (props, ref) => {
//...
useImperativeHandle(ref, () => ({
// each key is connected to `ref` as a method name
// they can execute code directly, or call a local method
method1: () => { localMethod1() },
method2: () => { console.log("Remote method 2 executed") }
}))
//...
// These are local methods, they are not seen by `ref`,
const localMethod1 = () => {
console.log("Method 1 executed")
}
// ..
}
forwardRef
导出子组件:const ChildComponent = (props, ref) => {
// ...
}
export default forwardRef(ChildComponent)
子组件
import React, { useImperativeHandle, forwardRef } from 'react';
import { View } from 'react-native'
const ChildComponent = (props, ref) => {
useImperativeHandle(ref, () => ({
// methods connected to `ref`
sayHi: () => { sayHi() }
}))
// internal method
const sayHi = () => {
console.log("Hello")
}
return (
<View />
);
}
export default forwardRef(ChildComponent)
父组件
import React, { useRef } from 'react';
import { Button, View } from 'react-native';
import ChildComponent from './components/ChildComponent';
const App = () => {
const childRef = useRef()
return (
<View>
<ChildComponent ref={childRef} />
<Button
onPress={() => {
childRef.current.sayHi()
}}
title="Execute Child Method"
/>
</View>
)
}
export default App
Expo Snacks 上有一个互动演示: https://snack.expo.dev/@backupbrain/calling-functions-from-other-components
这个解释是从这个TutorialsPoint article