Skip to content
This repository was archived by the owner on Sep 1, 2024. It is now read-only.
This repository was archived by the owner on Sep 1, 2024. It is now read-only.

Upgrading defaultProps to use JavaScript default parameters shows isRequired errors #413

Open
@missalyss

Description

@missalyss

React version: 18.3.1
Prop-Types version: 15.8.1

Context:

Currently using Prop-Types for documenting UI pattern library components as well as for in-console errors to ensure consumers of component library use important props (like aria-label, for example).

Problem:

Recent version of react has deprecated defaultProps for functional components. The deprecation is flagged as a "warning" in the console during development (but is not type warning, but type error). These errors are disruptive to development as they take up a lot of space for every instance and since they're type error, not easily ignored or filtered out.

 Support for defaultProps will be removed from function components in a future major release. Use JavaScript default parameters instead.
image

As instructed, I removed defaultProps and put the default value in as a default parameter in props like so:

const Icon = ({
  viewBox = '0 0 24 24',
}) => {
    ...
};

Icon.propTypes = {
  viewBox: PropTypes.string.isRequired,
};

export default Icon;

The component renders and behaves correctly. However, the following error is then displayed in my console:

Failed prop type: The prop `viewBox` is marked as required in `Icon`, but its value is `undefined`.
image

Relevant code: https://github.com/facebook/prop-types/blob/main/factoryWithTypeCheckers.js#L211

Expected: migrating to default parameters gets rid of defaultProps error while isRequired reads default parameters as default props.

Actual: isRequired does not read default parameters as default props but sees that the props are undefined.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions