I have a small React Component where the child is a function. I can't seem to figure out how to define the children prop in TypeScript.
class ClickOutside extends React.PureComponent<Props, {}> {
render() {
const { children } = this.props;
return children({ setElementRef: this.setElementRef });
}
}
I'm calling it this way:
<ClickOutside onOutsideClick={() => this.setState({ isOpen: false })}>
{({ setElementRef }) => (
<div ref={setElementRef}>
{trigger}
{children}
</div>
)}
</ClickOutside>
And this is what I tried to define the prop but it does not work at all. I have tried several other things but failed and I'm kinda lost here.
type Props = {
children: ({ setElementRef: (el: HTMLElement) => any }) => React.ReactNode,
onOutsideClick: Function,
}
EDIT:
This produces ';' expected. on =>
When I change the type definition this way:
type Props = {
children: ({ setElementRef: (el: HTMLElement) => any }),
onOutsideClick: Function,
}
It produces this error:
Cannot invoke an expression whose type lacks a call signature. Type '{ setElementRef: (el: HTMLElement) => any; } | ({ setElementRef: (el: HTMLElement) => any; } & string) | ({ setElementRef: (el: HTMLElement) => any; } & number) | ({ setElementRef: (el: HTMLElement) => any; } & false) | ({ ...; } & true) | ({ ...; } & ReactElement<...>) | ({ ...; } & ReactNodeArray) | ({ ...; } & Re...' has no compatible call signatures.
React.PureComponenttoReact.Component