I am receiving the warning Warning: Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()?. I'm confused because I am using forwardRef()... and it is working.
I'm attempting to pass my custom Input Element to ReactDatePicker. There are several GitHub issues on this such as this one. But I can't work through this last error while implementing the examples on there that work.
Here is the custom Input element:
interface InputProps extends InputHTMLAttributes<HTMLInputElement> {
ref?: React.Ref<HTMLInputElement>;
}
const StyledInput = styled.input<InputProps>`
box-sizing: border-box;
// ...
`;
export const Input: FunctionComponent<InputProps> = (props: InputProps) => {
return (
<>
<StyledInput {...props}></StyledInput>
</>
);
};
And here is the custom DatePicker with ReactDatePicker where the error occurs:
interface DatePickerProps extends ReactDatePickerProps {
//... custom props
}
const StyledDatePicker = styled(ReactDatePicker)`
//... some CSS
`;
const CustomInput = forwardRef<HTMLInputElement>((inputProps, ref) => (
<Input {...inputProps} ref={ref} /> // <-- error occurs here
));
export const DatePicker: FunctionComponent<DatePickerProps> = (props: DatePickerProps) => {
const ref = React.createRef<HTMLInputElement>();
return (
<>
<StyledDatePicker
{...props}
customInput={<CustomInput ref={ref} />}
></StyledDatePicker>
</>
);
};