React16.8中父组件获取子组件数据的3中方式

栏目: IOS · Android · 发布时间: 6年前

内容简介:在函数组件中要获取子组件的数据,需要两步骤1.将上面的方式都会将组件中全部的数据暴露出去,有时候我们想只想暴露出一部分数据
  • 1、定义父组件(直接使用 ref )

    export default class UserRef1 extends Component {
      constructor(props) {
        super(props);
        this.child = React.createRef();
      }
      focus = () => {
        console.log(this.child.current.inputRef.current.value);
        this.child.current.inputRef.current.focus();
      }
      render() {
        return (
          <div>
            <Child ref={this.child} />
            <button onClick={this.focus}>获取焦点</button>
          </div>
        )
      }
    }
    复制代码
  • 2、子组件中

    class Child extends Component {
      constructor(props) {
        super(props);
        this.state = {
          name: '哈哈'
        }
        this.inputRef = React.createRef();
      }
      render() {
        return (
          <input type="text" value={this.state.name} onChange={(e) => this.setState(e.target.value)} ref={this.inputRef} />
        )
      }
    }
    复制代码

二、函数组件

在函数组件中要获取子组件的数据,需要两步骤1.将 ref 传递到子组件中,2.需要使用 forwardRef 对子组件进行包装

  • 1、父组件

    export default () => {
      const parentRef = useRef();
      function focusHander() {
        console.log(parentRef);
        parentRef.current.focus();
        parentRef.current.value = '哈哈';
      }
      return (
        <>
          <ForwardChild ref={parentRef} />
          <button onClick={focusHander}>获取焦点</button>
        </>
      )
    }
    复制代码
  • 2、子组件中

    function Child(props, parentRef) {
      console.log(props);
      return (
        <>
          <input type="text" ref={parentRef} />
        </>
      )
    }
    /**
     * 使用forwardRef将ref直接传递进去
     */
    let ForwardChild = forwardRef(Child);
    复制代码

三、优化

上面的方式都会将组件中全部的数据暴露出去,有时候我们想只想暴露出一部分数据

  • 1、子组件的代码

    import React, { useState, useRef, useImperativeHandle, forwardRef } from 'react'
    
    function Child(props, parentRef) {
      const inputRef = useRef();
      useImperativeHandle(parentRef, () => {
        // return返回的值就可以被父组件获取到
        return {
          focus() {
            inputRef.current.focus();
          }
        }
      })
      return (
        <>
          <p>{props.name}</p>
          <input type="text" ref={inputRef} />
        </>
      )
    }
    
    let ForwardChidl = forwardRef(Child);
    复制代码
  • 2、父组件的代码

    export default () => {
      const parentRef = useRef();
    
      const focusHandler = () => {
        parentRef.current.focus();
      }
      return (
        <>
          <ForwardChidl ref={parentRef} name={'你好'} />
          <button onClick={focusHandler}>获取焦点</button>
        </>
      )
    }
    复制代码

以上所述就是小编给大家介绍的《React16.8中父组件获取子组件数据的3中方式》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

心理学与生活

心理学与生活

[美] 理查德·格里格、菲利普·津巴多 / 王垒、王甦 等 / 人民邮电出版社 / 2003-10 / 88.00元

《心理学与生活》是美国斯坦福大学多年来使用的教材,也是在美国许多大学里推广使用的经典教材,被ETS推荐为GRE心理学专项考试的主要参考用书,还是被许多国家大学的“普通心理学”课程选用的教材。这本教科书写作流畅,通俗易懂,深入生活,把心理学理论与知识联系人们的日常生活与工作,使它同样也成为一般大众了解心理学与自己的极好读物。 作为一本包含着丰富的教育思想和独特教学方法的成熟教材,原书中所有元素......一起来看看 《心理学与生活》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

html转js在线工具
html转js在线工具

html转js在线工具