简体   繁体   English

使用 React.forwardRef 与自定义 ref 道具的价值

[英]value of using React.forwardRef vs custom ref prop

I see that React.forwardRef seems to be the sanctioned way of passing a ref to a child functional component, from the react docs:我从反应文档中看到 React.forwardRef 似乎是将 ref 传递给子功能组件的认可方式:

const FancyButton = React.forwardRef((props, ref) => (
  <button ref={ref} className="FancyButton">
    {props.children}
  </button>
));

// You can now get a ref directly to the DOM button:
const ref = React.createRef();
<FancyButton ref={ref}>Click me!</FancyButton>;

However, what is the advantage of doing this over simply passing a custom prop?:但是,与简单地传递自定义道具相比,这样做的优势是什么?:

const FancyButton = ({ innerRef }) => (
  <button ref={innerRef} className="FancyButton">
    {props.children}
  </button>
));

const ref = React.createRef();
<FancyButton innerRef={ref}>Click me!</FancyButton>;

The only advantage I can think of is maybe having a consistent api for refs, but is there any other advantage?我能想到的唯一优势可能是对裁判具有一致的 api ,但还有其他优势吗? Does passing a custom prop affect diffing when it comes to rendering and cause additional renders, surely not as the ref is stored as mutable state in the current field?在渲染时传递自定义道具是否会影响差异并导致额外的渲染,当然不会因为 ref 在current字段中存储为可变的 state ?

Say for example you wanted to pass multiple refs (which tbh, might indicate code smell, but still), then the only solution I can see would be to use customRef props.例如,假设您想传递多个 ref(这可能表明代码有异味,但仍然如此),那么我能看到的唯一解决方案就是使用 customRef 道具。

I guess my question is what is the value of using forwardRef over a custom prop?我想我的问题是在自定义道具上使用forwardRef的价值是什么?

Even React docs mention the custom ref prop as a more flexible approach to forwardRef :甚至React 文档也提到自定义 ref 属性是一种更灵活的forwardRef方法:

If you use React 16.2 or lower, or if you need more flexibility than provided by ref forwarding , you can use this alternative approach and explicitly pass a ref as a differently named prop .如果您使用 React 16.2 或更低版本,或者如果您需要比 ref forwarding 提供的更多灵活性,您可以使用这种替代方法并将ref 作为不同命名的 prop显式传递。

There is also a gist , in which Dan Abramov writes about its advantages:还有一个要点,Dan Abramov 在其中写到了它的优点:

  • compatible with all React versions兼容所有 React 版本
  • works for class and function components适用于 class 和 function 组件
  • simplifies passing a ref to a nested component several layers deep简化了将 ref 传递给嵌套组件几层的过程

I would add, that passing refs as usual props does not cause breaking changes and is the way to go for multiple refs.我要补充一点,像往常一样传递 refs 道具不会导致重大变化,并且是 go 用于多个 refs 的方式。 The only advantages of forwardRef coming to my mind are:我想到的forwardRef的唯一优点是:

  • uniform access API for DOM nodes, functional and class components (you mentioned that)统一访问 API 用于 DOM 节点、功能和 class 组件(您提到过)
  • ref attribute does not bloat your props API, eg if you provide types with TypeScript ref属性不会使您的道具 API 膨胀,例如,如果您提供带有 TypeScript 的类型

Does passing a custom prop affect diffing when it comes to rendering and cause additional renders?传递自定义道具是否会影响渲染时的差异并导致额外的渲染?

A ref can potentially trigger a re-render if you pass an inline callback ref function down as prop.如果您将内联回调 ref function 作为 prop 向下传递,则 ref 可能会触发重新渲染。 But it is a better idea anyway to define it as class instance method or via some memoization like useCallback .但无论如何,将其定义为 class 实例方法或通过一些像useCallback这样的记忆是一个更好的主意

Ref is a standard property in React components. RefReact组件中的标准属性。

Some components that wrap other components to provide additional functionality, use ref to refer to wrapped component and expect that the component has ref property.一些包装其他组件以提供附加功能的组件,使用ref来引用被包装的组件并期望该组件具有ref属性。

It is better for a component to have the ref property to be compatible with other components and libraries.组件最好具有ref属性以与其他组件和库兼容。

Function components cannot have the "ref" property and must use forwardRef instead to provide ref property. Function 组件不能具有“ref”属性,必须使用forwardRef来提供ref属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM