Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: React.forwardRef component defaultProps not working #25618

Closed
XenoBino opened this issue Nov 2, 2022 · 3 comments
Closed

Bug: React.forwardRef component defaultProps not working #25618

XenoBino opened this issue Nov 2, 2022 · 3 comments
Labels
Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug

Comments

@XenoBino
Copy link

XenoBino commented Nov 2, 2022

React version: 18.2.0

Steps To Reproduce

import { forwardRef } from "react";

export interface PropsType {
  name: string,
}

const RefTest = forwardRef<HTMLInputElement, PropsType>((props, ref) => {
  return (
      <input
	    type="text"
		placeholder="test"
		ref={ref}
	  />
  );
});

RefTest.defaultProps = {
  name: "test"
};

export { RefTest };

<RefTest /> // Property 'name' is missing in type '{}' but required in type 'PropsType'.

The current behavior

Property 'name' is missing in type '{}' but required in type 'PropsType'.

The expected behavior

It should compile fine. This code is similar to the one suggested in #16653 (link to comment)

@XenoBino XenoBino added the Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug label Nov 2, 2022
@znareak
Copy link

znareak commented Nov 3, 2022

You should check this posts, maybe you can fix your problem:

#16653
typescript-cheatsheets/react#199

@eps1lon
Copy link
Collaborator

eps1lon commented Nov 3, 2022

Closing since this is a usage question related to TypeScript that was answered by znareak

@eps1lon eps1lon closed this as completed Nov 3, 2022
@XenoBino
Copy link
Author

XenoBino commented Nov 4, 2022

I insist that there IS a problem. Using the answer in the link above raises another issue, props (from the component's perspective) are now nullable, meaning that you need to null check them. defaultProps are supposed to provide the component with defaults so that it doesn't need to null check. For example:

import React, { useState } from "react";

const defaultProps = {
  validate: (value: string) => (value === "hello")
};

export type TestProps = {
  firstName: string,
  lastName: string
} & Partial<typeof defaultProps>;

const TestRef = React.forwardRef<HTMLInputElement, TestProps>((props, ref) => {
  const [valid, setValid] = useState(false);

  return (
    <>
      <div>{props.firstName} {props.lastName}</div>
      <input
        ref={ref}
        onChange={ (e) => {
          setValid(props.validate(e.target.value)) // ■ Cannot invoke an object which is possibly 'undefined'.
        }}
      />
      <span>{ valid ? "valid" : "invalid" }</span>
    </>
  );
});

TestRef.defaultProps = defaultProps;

export { TestRef };

The error which says Cannot invoke an object which is possibly 'undefined' is not supposed to happen as the value will always be defined (thanks to defaultProps). If I put a @ts-ignore in front of it it goes and defaultProps function without a problem. This suggests that there IS a problem with the type definitions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug
Projects
None yet
Development

No branches or pull requests

3 participants