Search code examples
reactjstypescriptreact-typescript

Property 'onClick' does not exist on type '{ children?: ReactNode; }'


I can't seem to find what I'm doing wrong... Any help will be appreciated:

type Props = {
  onClick: () => void, 
  value: string
}

const CustomInput = forwardRef<Props>(({ onClick, value }, ref) => (
  <div className="react-datepicker-custom-input" onClick={onClick}>
    {value}
    <i className={classes.arrowDown}></i>
  </div>
));


error message: Property 'onClick' does not exist on type '{ children?: ReactNode; }

export default CustomInput;


Solution

  • Props generic should go as a second argument, not the first.

    Example:

    type Props = {
      onClick: () => void, 
      value: string
    }
    
    type RefType=number
    const CustomInput = forwardRef<RefType, Props>(({ onClick, value }, ref) => (
      <div className="react-datepicker-custom-input" onClick={onClick}>
        {value}
        <i className={classes.arrowDown}></i>
      </div>
    ));
    

    First generic argument of forwardRef is for ref type, second - if for props accordingly