-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Support ref-forwarding #640
Comments
Just use React.forwardRef when it is needed. Wrap any enhanced component whenever needed as like as in React doc https://reactjs.org/docs/forwarding-refs.html |
Hey, everyone!
|
Indeed, reading documentation helps! I've found out a way to do this.
|
Hi. Just faced same issue, that is not possible to workaround. I think issue is in When using together with another HOC, that adds ref, I'm not sure how I can workaround it, except adding For example, This gives refs warning: compose(
DragLayer,
branch,
); This does not gives refs warning: compose(
DragLayer,
toClass,
branch,
); |
This is relatively easy to add support in your own code base, but I think it'd be nice to have as a helper here as well. Here's what I'm doing right now: const enhance: HOC<*, EnhancedProps> = compose(
forwardRefs(),
mapProps(/* ... */),
restoreRefs(),
); /* @flow */
/* eslint-disable react/no-multi-comp */
import React, {
type ComponentType,
} from 'react';
import {
getDisplayName,
compose as baseCompose,
type HOC,
} from 'recompose';
import hoistStatics from 'hoist-non-react-statics';
export const compose: $Compose = ((...funcs: *) => (
baseCompose(
forwardRefs(),
...funcs,
restoreRefs(),
)
): any);
export const forwardRefs = <Base, Enhanced>({
propName = 'forwardedRef',
}: {
propName: string,
} = {}): HOC<Base, Enhanced> => ((
Component: ComponentType<Base>,
): ComponentType<Enhanced> => {
const forwarder = (props: *, ref: *) => (
<Component { ...{ [propName]: ref, ...props } } />
);
const Container = (React: any).forwardRef(forwarder);
const displayName = getDisplayName(Component);
forwarder.displayName = displayName;
Container.displayName = displayName;
hoistStatics(Container, Component);
return Container;
});
export const restoreRefs = <Base, Enhanced>({
propName = 'forwardedRef',
}: {
propName: string,
} = {}): HOC<Base, Enhanced> => ((
Component: ComponentType<Base>,
): ComponentType<Enhanced> => {
const Container = ({ [propName]: ref, ...props }: *) => (
<Component ref={ ref } { ...props } />
);
Container.displayName = getDisplayName(Component);
return (Container: any);
}); |
After much searching, I ended up creating a const withForwardingRef = <Props extends {[_: string]: any}>(BaseComponent: React.ReactType<Props>) =>
React.forwardRef((props, ref) => <BaseComponent {...props} forwardedRef={ref} />);
export default withForwardingRef; usage: const Comp = ({forwardedRef}) => (
<button ref={forwardedRef} />
)
const MyBeautifulComponent = withForwardingRef<Props>(Comp); // Now Comp has a prop called forwardedRef usage of usage: <MyBeautifulComponent ref={someRef} /> |
Hello together!
Since the newest React-Version forwarding of refs is supported. Should this be used in this library? This would make the recompose HOCs truly transparent. This would be a breaking change of the API, because some users might be using the lifecycle HOC to add public methods to their instances. This would no longer work, when all recompose HOCs forward refs.
The text was updated successfully, but these errors were encountered: