简体   繁体   English

React v16 – 通过包装组件将 ref 从子级传递给父级

[英]React v16 – pass ref from child to parent through a wrapper component

I have an existing component hierarchy that looks like this:我有一个现有的组件层次结构,如下所示:

const Parent = props => {
  const divElement = useRef()
  // do something with divElement
  return <Child ref={divElement} {...some props} />
}

const Child = React.forwardRef((props, ref) => {
  return <div ref={ref}><some stuff here></div>
}

This is all fine, but now I need to conditionally add a wrapper component, which wraps the <Child> component to add some special case props.这一切都很好,但是现在我需要有条件地添加一个包装器组件,它包装<Child>组件以添加一些特殊情况的道具。

Basically what I want is something like this:基本上我想要的是这样的:

const Parent = props => {
  const divElement = useRef()
  // do something with divElement
  return someCondition ? <Wrapper {...some props} /> : <Child ref={divElement} {...some props} />
}

const Wrapper = props => {
  return <Child {...different props} />
}

const Child = React.forwardRef((props, ref) => {
  return <div ref={ref}><some stuff here></div>
}

I'm stuck on how to pass the ref from Child through the Wrapper back to Parent , so the Parent can access the Child 's ref no matter whether the Wrapper is there or not...我一直坚持如何将Child通过Wrapperref传递回Parent ,因此无论Wrapper是否存在, Parent都可以访问Childref ......

You shouldn't pass anything from child to parent.您不应该将任何东西从孩子传递给父母。 Of course you can use callbacks, but you wouldn't use them to pass refs or something similar.当然你可以使用回调,但你不会使用它们来传递 refs 或类似的东西。

You just should make the Wrapper also a ForwardRef:您只需要将 Wrapper 也设为 ForwardRef:

const Wrapper = React.forwardRef((props, ref) => {
    return <Child ref={ref} {...your other props} />
})

Now you can just call:现在你可以打电话:

return someCondition ? <Wrapper ref={divElement} {...some props} /> : <Child ref={divElement} {...some props} />

and your ref will always be the div you want.你的 ref 将永远是你想要的 div。

In addition to using React.forwardRef like @Reductio said, you could use custom props to pass down the ref to the children whether with the Wrapper or not.除了像@Reductio 说的那样使用React.forwardRef之外,您还可以使用自定义道具将 ref 传递给孩子,无论是否使用 Wrapper。

I learned this from here: https://deniapps.com/blog/clarify-react-ref-by-examples Passing ref via custom props is much simpler.我从这里学到了这一点: https://deniapps.com/blog/clarify-react-ref-by-examples通过自定义道具传递 ref 要简单得多。 The code likes this:代码是这样的:

const Parent = props => {
  const divElement = useRef()
  // do something with divElement
  return someCondition ? <Wrapper {...some props} forwardedRef={divElement} /> : <Child forwardedRef={divElement} {...some props} />
}

const Wrapper = props => {
  //MAKE SURE "different props" includes the custom props: forwardedRef from props
  return <Child {...different props} />
}

const Child =({forwardRef, ...rest}) => {
  return <div ref={forwardedRef} {...rest}><some stuff here></div>
}

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

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