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

Mysql example of splitting into multiple rows and columns by specific symbols

Some fault code tables use the following design p...

js to achieve star flash effects

This article example shares the specific code of ...

Steps to build MHA architecture deployment in MySQL

Table of contents MAH 1. Introduction to MAH Arch...

Talk about nextTick in Vue

When the data changes, the DOM view is not update...

40 fonts recommended for famous website logos

Do you know what fonts are used in the logo desig...

Linux system command notes

This article describes the linux system commands....

Example of creating table statements for user Scott in MySQL version of Oracle

Overview: Oracle scott user has four tables, whic...

How to completely delete the MySQL service (clean the registry)

Preface When installing the executable file of a ...

Detailed explanation of the use of grid properties in CSS

Grid layout Attributes added to the parent elemen...

JavaScript implements an input box component

This article example shares the specific code for...

Implementing simple tabs with js

Tab selection cards are used very frequently on r...

Things to note when designing web pages for small-screen mobile devices

The reason is that this type of web page originate...

CSS margin overlap and how to prevent it

The vertically adjacent edges of two or more bloc...

5 things to note when writing React components using hooks

Table of contents 01. Use useState when render is...