Skip to content

Instantly share code, notes, and snippets.

@itsdouges
Created April 16, 2018 13:10
Show Gist options
  • Save itsdouges/42af1b5b53d84a9ba2a9791980e36d0a to your computer and use it in GitHub Desktop.
Save itsdouges/42af1b5b53d84a9ba2a9791980e36d0a to your computer and use it in GitHub Desktop.
Collects a descendants refs up the tree and calls back with it.
import * as React from 'react';
type SupplyRef = (ref: HTMLElement | null) => void;
type GetRef = SupplyRef;
type ChildrenGetRef = (getRef: SupplyRef) => React.ReactNode;
interface Props {
children: ChildrenGetRef | React.ReactNode;
getRef?: GetRef;
}
const RefContext = (React as any).createContext();
export default class RefCollector extends React.Component<Props> {
render() {
if (typeof this.props.children !== 'function') {
return this.props.getRef ? (
<RefContext.Consumer>
{(setRefFunc?: SupplyRef) => (
<RefContext.Provider
value={(ref: HTMLElement | null) => {
this.props.getRef && this.props.getRef(ref);
setRefFunc && setRefFunc(ref);
}}
>
{this.props.children}
</RefContext.Provider>
)}
</RefContext.Consumer>
) : (
this.props.children
);
}
return (
<RefContext.Consumer>
{(setRefFunc?: SupplyRef) =>
typeof this.props.children === 'function' &&
this.props.children((ref: HTMLElement) => {
setRefFunc && setRefFunc(ref);
this.props.getRef && this.props.getRef(ref);
})
}
</RefContext.Consumer>
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment