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中方式》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

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

新零售进化论

新零售进化论

陈欢、陈澄波 / 中信出版社 / 2018-7 / 49.00

本书主要介绍了新零售的进化现象和规律,提出了新零售的第一性原理是物理数据二重性,即在新零售时代,所有的人、货、场既是物理的也是数据的。 通过这个原点,进一步衍生出了新零售的八大核心算法,并用大量的辅助观点和新零售案例来揭示新零售背后的算法逻辑。 综合一系列的理论推演和案例讲解,作者重点回答了以下3个问题: ● 我们是行业的强者,如果跟不上新零售的潮流,会不会被淘汰? ● 我......一起来看看 《新零售进化论》 这本书的介绍吧!

URL 编码/解码
URL 编码/解码

URL 编码/解码

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换