React 父组件调用子组件的方法

发布于 2023-08-27 11:50:57 字数 3801 浏览 34 评论 0

在 React 中,我们经常在子组件中调用父组件的方法,一般用 props 回调即可。但是有时候也需要在父组件中调用子组件的方法,通过这种方法实现高内聚。有多种方法,请按需服用。

类组件中

React.createRef()

  • 优点:通俗易懂,用 ref 指向。
  • 缺点:使用了 HOC 的子组件不可用,无法指向真是子组件

比如一些常用的写法,mobx 的 @observer 包裹的子组件就不适用此方法。

import React, { Component } from 'react';

class Sub extends Component {
  callback() {
    console.log('执行回调');
  }
  render() {
    return <div>子组件</div>;
  }
}

class Super extends Component {
  constructor(props) {
    super(props);
    this.sub = React.createRef();
  }
  handleOnClick() {
    this.sub.callback();
  }
  render() {
    return (
      <div>
        <Sub ref={this.sub}></Sub>
      </div>
    );
  }
}

ref 的函数式声明

  • 优点:ref 写法简洁
  • 缺点:使用了 HOC 的子组件不可用,无法指向真是子组件(同上)

使用方法和上述的一样,就是定义 ref 的方式不同。

...

<Sub ref={ref => this.sub = ref}></Sub>

...

使用 props 自定义 onRef 属性

  • 优点:假如子组件是嵌套了 HOC,也可以指向真实子组件。
  • 缺点:需要自定义 props 属性
import React, { Component } from 'react';
import { observer } from 'mobx-react'

@observer
class Sub extends Component {
	componentDidMount(){
    // 将子组件指向父组件的变量
		this.props.onRef && this.props.onRef(this);
	}
	callback(){
		console.log("执行我")
	}
	render(){
		return (<div>子组件</div>);
	}
}

class Super extends Component {
	handleOnClick(){
       // 可以调用子组件方法
		this.Sub.callback();
	}
	render(){
		return (
          <div>
			<div onClick={this.handleOnClick}>click</div>
			<Sub onRef={ node => this.Sub = node }></Sub>
	   	  </div>)
	}
}

函数组件、Hook 组件

useImperativeHandle

  • 优点: 1、写法简单易懂 2、假如子组件嵌套了 HOC,也可以指向真实子组件
  • 缺点: 1、需要自定义 props 属性 2、需要自定义暴露的方法
import React, { useImperativeHandle } from 'react';
import { observer } from 'mobx-react'


const Parent = () => {
  let ChildRef = React.createRef();

  function handleOnClick() {
    ChildRef.current.func();
  }

  return (
    <div>
      <button onClick={handleOnClick}>click</button>
      <Child onRef={ChildRef} />
    </div>
  );
};

const Child = observer(props => {
  //用 useImperativeHandle 暴露一些外部 ref 能访问的属性
  useImperativeHandle(props.onRef, () => {
    // 需要将暴露的接口返回出去
    return {
      func: func,
    };
  });
  function func() {
    console.log('执行我');
  }
  return <div>子组件</div>;
});

export default Parent;

forwardRef

使用 forwardRef 抛出子组件的 ref

这个方法其实更适合自定义 HOC。但问题是,withRouter、connect、Form.create 等方法并不能抛出 ref,假如 Child 本身就需要嵌套这些方法,那基本就不能混着用了。forwardRef 本身也是用来抛出子元素,如 input 等原生元素的 ref 的,并不适合做组件 ref 抛出,因为组件的使用场景太复杂了。

import React, { useRef, useImperativeHandle } from 'react';
import ReactDOM from 'react-dom';
import { observer } from 'mobx-react'

const FancyInput = React.forwardRef((props, ref) => {
  const inputRef = useRef();
  useImperativeHandle(ref, () => ({
    focus: () => {
      inputRef.current.focus();
    }
  }));

  return <input ref={inputRef} type="text" />
});

const Sub = observer(FancyInput)

const App = props => {
  const fancyInputRef = useRef();

  return (
    <div>
      <FancyInput ref={fancyInputRef} />
      <button
        onClick={() => fancyInputRef.current.focus()}
      >父组件调用子组件的 focus</button>
    </div>
  )
}

export default App;

总结

父组件调子组件函数有两种情况

  • 子组件无 HOC 嵌套:推荐使用 ref 直接调用
  • 有 HOC 嵌套:推荐使用自定义 props 的方式

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

关于作者

帥小哥

暂无简介

文章
评论
27 人气
更多

推荐作者

櫻之舞

文章 0 评论 0

弥枳

文章 0 评论 0

m2429

文章 0 评论 0

野却迷人

文章 0 评论 0

我怀念的。

文章 0 评论 0

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文