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

Add prop types for react html elements #285

Closed
Tommydreamer57 opened this issue Aug 8, 2019 · 3 comments
Closed

Add prop types for react html elements #285

Tommydreamer57 opened this issue Aug 8, 2019 · 3 comments

Comments

@Tommydreamer57
Copy link

Tommydreamer57 commented Aug 8, 2019

Is there a way to add proptypes for plain react (HTML) elements, in a way similar to this?

MyButton.propTypes = {
    onClick: PropTypes.func,
    buttonProps: PropTypes.HTMLButtonProps,
};
function MyButton({ onClick, buttonProps }) {
    return (
        <button
            onClick={onClick}
            {...buttonProps}
        />
    );
}

Or even better, to simply spread those props into the prop types like this:

MyButton.propTypes = {
    // onClick: PropTypes.func,
    ...PropTypes.HTMLButtonProps,
};
function MyButton({ onClick, ...buttonProps }) {
    return (
        <button
            onClick={onClick}
            {...buttonProps}
        />
    );
}

Obviously, the examples are kind of useless, but for large components that spread certain props into html elements it would be very useful to be able to declare prop types for those props.

Thank you!

@ljharb
Copy link
Contributor

ljharb commented Aug 8, 2019

That would be a massive effort to maintain, since the list of valid attributes per html element changes all the time. (Separately, it’s generally considered a bad practice to spread props onto html elements - favor explicit over implicit)

It’s certainly possible, but there’s so many html elements with so large and complex lists of attributes and attribute value validations that it’s probably best done as a separate project.

@Tommydreamer57
Copy link
Author

That makes sense, however I assumed react already has somewhat of a list somewhere since vscode already provides really nice autocomplete when typing plain elements in react.

image

Do you know where this list comes from?

I'll take into account your suggestion not to spread props into html elements. That actually sounds like a better solution. Thanks

@ljharb
Copy link
Contributor

ljharb commented Aug 8, 2019

If React core exposed that list - which I wish it would, but it doesn't - then it might indeed be much easier to make the propType :-)

For now, though, I'm going to close this.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants