比如说,我有一个具有可滚动子组件的组件,我想展示滚动的能力:

const MyComponent = (props) => {
    return <ScrollView ... />
}

我希望能够做到

<MyComponent ref={myRef} />

...

myRef.scrollTo({x: 0});

所以我需要一个方法把裁判转到<ScrollView>.让我们试着把裁判放在props 上:

const MyComponent = (props) => {
    return <ScrollView ref={props.scrollRef} ... />
}

...

<MyComponent scrollRef={myRef} />

...

myRef.scrollTo({x: 0});

我只是在iOS上用React Nativetry 了一下,它确实有效.我看到了超过React.forwardRef的优势:

  • 更简单,因为我不需要使用其他React API.
  • 如果有多个子元素需要转发ref,也可以使用.
  • 在我看来,这种方法是可行的

React.forwardRef的优势是什么?为什么要在React 16.3中添加它?

推荐答案

请注意,使用另一个命名的props (如innerRef FOR FORWARDING)没有区别,它的工作原理相同.


重构类组件

由于React向函数组件(钩子)移动,您可能希望将类组件代码重构为函数组件without breaking the API.

// Refactor class component API to function component using forwardRef
<Component ref={myRef} />

React.forwardRef将是您唯一的 Select (详细说明).

清洁空气污染指数

作为一名图书馆作者,你需要一个predictable API,用于ref次转发.

例如,如果您实现了Componentsomeone wants to attach a ref to it,他有两个选项,具体取决于您的API:

<Component innerRef={myRef} />
  • 开发者需要知道有一个自定义的转发props
  • innerRef号附加在哪个元素上?我们不知道,应该在API中提及还是我们console.log(myRef.current)

<Component ref={myRef} />
  • 默认行为类似于HTML元素上使用的ref prop,通常附加到内部包装器组件.

请注意,React.forwardRef can be used用于函数组件和HOC(用于类组件see alternative below).

Ref转发不限于DOM组件.也可以将引用转发到类组件实例.

对于函数组件,forwardRef有时附带useImperativeHandle combo(在类组件中,您只需调用ref instance:ref.current.myAttr()上的类方法).

// Same usage
<Component ref={myRef} />

const Component = React.forwardRef((props, ref) => {
  // you can forward ref <div ref={ref} />
  // you can add custom attributes to ref instance with `useImperativeHandle`
  // like having ref.myAttribute() in addition to ones attached to other component.
});

ref个props 中的102个没有forwardRef个.

对于类组件,仅此代码就将ref附加到CLASS INSTANCE,这本身并不有用,需要另一个ref来转发:

// usage, passing a ref instance myRef to class Component
<Component ref={myRef} />

完整示例,请查看日志(log):

// We want to forward ref to inner div
class ClassComponent extends React.Component {
  innerRef = React.createRef();
  render() {
    // Notice that you can't just `this.props.ref.current = node`
    // You don't have `ref` prop, it always `undefined`.
    return <div ref={this.innerRef}>Hello</div>;
  }
}

const Component = () => {
  const ref = React.useRef();

  useEffect(() => {
    // The ref attached to class instance
    console.log(ref.current);
    // Access inner div through another ref
    console.log(ref.current.innerRef);
  }, []);

  return <ClassComponent ref={ref} />;
};

Edit React Template (forked)

在函数组件中,它甚至不起作用,因为函数没有实例.

默认情况下,不能对函数组件使用ref属性,因为它们没有实例.[1]

React-native相关问答推荐

错误:HostBody::get for prop NativeUnimoduleProxy中出现异常- Android虚拟设备(AVD)使用Expo测试React Native App时崩溃

何时在 React Native 中使用辅助功能 Role='link'?

如何在 react-native 中使用 Animated 制作 svg 动画

reactnavigation后退按钮的自定义后退导航

Google SignIn SDK 因抛出错误而失败,发生不可恢复的登录失败 -catch 错误

IntelliSense 在第一项后无法在 React Native 样式表中工作

TabNavigator 中的 React Navigation 传递props

React Native 错误:Animated.event now requires a second argument for options

RNFS.exists() 总是返回 TRUE

使用像 React Navigation 这样的基于 JS 的导航解决方案优缺点?

React-Native iOS - 如何通过按下按钮从 React-Native 视图导航到非 React-Native 视图(本机 iOS 视图控制器)?

在 React Native 中仅显示第一行文本

react-native createbottomtabnavigator 隐藏标签栏标签

大型列表的 React-Native FlatList 性能问题

任务在 react-native 中因请求而成为orphaned - 这是什么意思?

检测是否通过推送通知打开了 React Native iOS 应用

我可以从react-native元素中删除 tvOS 吗?

React native - 以编程方式判断是否启用了远程 JS 调试

react-native构建错误:package android.support.annotation does not exist

如何在react-native中将文本放置在图像上?