专注于 JetBrains IDEA 全家桶,永久激活,教程
持续更新 PyCharm,IDEA,WebStorm,PhpStorm,DataGrip,RubyMine,CLion,AppCode 永久激活教程

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

一、类组件的情况下

  • 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中方式

JetBrains 全家桶,激活、破解、教程

提供 JetBrains 全家桶激活码、注册码、破解补丁下载及详细激活教程,支持 IntelliJ IDEA、PyCharm、WebStorm 等工具的永久激活。无论是破解教程,还是最新激活码,均可免费获得,帮助开发者解决常见激活问题,确保轻松破解并快速使用 JetBrains 软件。获取免费的破解补丁和激活码,快速解决激活难题,全面覆盖 2024/2025 版本!

联系我们联系我们