How to use React forwardRef and what to note

How to use React forwardRef and what to note

Previously, react.forwardRef could not be applied to react high-order components. I finally figured it out recently, so I wrote it down. The key point is that the ref in the React.forwardRef API must point to a dom element instead of a React component.

React.forwardRef usage examples

Here is an example of this error applied to a React component:

const A = React.forwardRef((props,ref)=><B {...props} ref={ref}/>)

This is the mistake I often made before. The ref here is not effective.

As mentioned earlier, ref must point to a DOM element, so the correct method is applied:

const A = React.forwardRef((props, ref) => (
<div ref={ref}>
<B {...props} />
</div>
))

Function and points for attention

  1. The parent component creates a ref object and binds it to the Dom element or class component in the child component
  2. Function components have no instances
  3. High-level components require special handling

The parent component obtains the Dom element instance in the child component

insert image description here

import React, { useRef } from 'react';
import Content from './content';

const Home = () => {
  // Create a Ref object const connectRef = useRef(null);

  const handleFoucus = () => {
    const _ref = connectRef.current;
    _ref.focus();
  };

  return (
    <div>
        <button onClick={() => handleFoucus()}>
          Use the method of DOM elements in child components</button>

        <Content ref={connectRef} />
    </div>
  );
};

export default Home;
import React, { forwardRef } from 'react';

/**
 * After forwardRef wraps, ref will be used as the second parameter to receive the passed ref attribute * eg
 * <Content count={count} user={user} ref={connectRef}>
 *
 * @param props - {count, user}
 * @param ref - connectRef
 * */
const Content = (props, ref) => {
  return (
    <div>
   	  {/* Bind ref to the passed ref ≈ ref={connectRef} */}
      <input type="password" ref={ref} />
    </div>
  )
};

export default forwardRef(Content);

The parent component obtains the class component instance in the child component

insert image description here

import React, { useRef } from 'react';
import Content from './content';

const Home = () => {
  // Create a Ref object const connectRef = useRef(null);

  const handleAdd = () => {
    const _ref = connectRef.current;

    const { count } = _ref.state;
    _ref.setState({
      count: count + 1
    })
  };

  return (
    <div>
        <button onClick={() => handleAdd()}>
          Use the properties and methods of the class component in the child component</button>

        <Content ref={connectRef} />
    </div>
  );
};

export default Home;
import React, { forwardRef } from 'react';
import Header from './header';
import Footer from './footer';

/**
 * After forwardRef wraps, ref will be used as the second parameter to receive the passed ref attribute * eg
 * <Content count={count} user={user} ref={connectRef}>
 *
 * @param props - {count, user}
 * @param ref - connectRef
 * */
const Content = (props, ref) => {
  return (
    <div>
      {/* Bind ref to the passed ref ≈ ref={connectRef} */}
      <Header ref={ref} /> {/* class component*/}
		
      {/* <Footer ref={ref} /> Function components have no instances, so connectRef.current: null */}
    </div>
  )
};

export default forwardRef(Content)
import React from 'react';

export default class Header extends React.Component {
  state = {
    count: 0
  };

  render() {
    return (
      <div>
        {this.state.count}
      </div>
    )
  }
};

Special cases in higher-order components

The high-order component will pass all received props to the wrapped component (transparent transmission)
Ref is similar to key. It is not a prop, so it will not be passed through. Ref will be bound to the outer packaging container.

/*
  Handling refs
  eg Hoc1(Hoc2(Content))

  <Content ref={myRef} /> The ref bound to Content will be bound to Hoc1 and will not be passed down to the first method React.forwardRef ================

      Use React.forwardRef() to process ref outside Hoc1 and use props to pass ref
      0. Wrap forwardRef outside the high-order component, intercept and obtain ref, add a props (xxx={ref}), and the real component is obtained through props.xxx1. Pass ref={XXXX} when using // Difference from the second method2. Use the second parameter of forwardRef to obtain ref
      3. Add a new props to forward ref downwards, eg forwardedRef={ref}
      4. Bind ref={props.forwardedRef} in the real component

      const Home = (props) => {
        const connectRef = useRef(null);

        return (
          <div>
            <Content ref={connectRef} />
          </div>
        );
      };

      // Wrapped component const Content = (props) => {
        return (
          <div>
            <input type="password" ref={props.forwardedRef} />
          </div>
        );
      };


      // The second input parameter of forwardRef can receive ref, and process ref in the outer layer of Hoc export default React.forwardRef((props, ref) => {
        const Wrapper = React.memo(Content); // Hoc

        // forwardRef wraps Wrapper
        // Need to pass ref down to the real component in Wrapper // Add a props attribute in Wrapper and pass the ref object as props to the child component return <Wrapper {...props} forwardedRef={ref} />;
      });

  The Second Method ==========

  0. Use a props to save ref when using
  1. Pass xxx={ref} when using // Difference from the first method 2. Bind ref={props.xxx} in the real component

  const Home = (props) => {
    const connectRef = useRef(null);

    return (
      <div>
        <Content forwardedRef={connectRef} />
      </div>
    );
  };

  // Define a higher-order component export const Hoc = (WrappedComponent) => {
    class Wrapper extends React.Component {
      render() {
        return <WrappedComponent {...props} />
      }
    }
  }

  // Wrapped component const Content = (props) => {
    return (
      <div>
        <input type="password" ref={props.forwardedRef} />
      </div>
    );
  };

  // Packaging process export default Hoc(Content);

* */

The above is the detailed content of the usage and precautions of React forwardRef. For more information on the use of React forwardRef, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • The complete usage of setup, ref, and reactive in Vue3 combination API
  • How to deeply understand React's ref attribute
  • Detailed explanation of the use of Refs in React's three major attributes
  • Specific use of useRef in React
  • React ref usage examples
  • Detailed explanation of how to use Ref in React
  • Detailed explanation of the use of react-refetch
  • Learn two demo examples of ref in React
  • Tutorial on using refs in React
  • Detailed explanation of the use of React component refs
  • RefreshContorl pull-down refresh usage in React Native
  • Detailed explanation of cross-usage of Ref in React

<<:  Summary of MySQL Architecture Knowledge Points

>>:  Summary of using MySQL online DDL gh-ost

Recommend

The problem of form elements and prompt text not being aligned

Recent projects involve the creation of a lot of ...

C# implements MySQL command line backup and recovery

There are many tools available for backing up MyS...

Details of using vue activated in child components

Page: base: <template> <div class="...

Implementing Markdown rendering in Vue single-page application

When rendering Markdown before, I used the previe...

Summary of the use of element's form elements

There are many form elements. Here is a brief sum...

Use auto.js to realize the automatic daily check-in function

Use auto.js to automate daily check-in Due to the...

Detailed process of using vmware to test PXE batch installation server

Table of contents 1. Preparation 1. Prepare the e...

Installation tutorial of MySQL 5.7.17 zip package version under win10

The installation tutorial of mysql5.7.17 is share...

How familiar are you with pure HTML tags?

The following HTML tags basically include all exis...

Vue Element-ui implements tree control node adding icon detailed explanation

Table of contents 1. Rendering 2. Bind data and a...

MySql knowledge points: transaction, index, lock principle and usage analysis

This article uses examples to explain the princip...

25 Examples of News-Style Website Design

bmi Voyager Pitchfork Ulster Grocer Chow True/Sla...