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.

Clarify Usage of elementType #334

Closed
Closed
@gbroques

Description

@gbroques

According to the Usage section in the README, elementType is for a "React element type (ie. MyComponent)".

// A React element type (ie. MyComponent).
optionalElementType: PropTypes.elementType,

However, I can also pass a string corresponding to the name of a DOM element (e.g. "span", "div", etc.) without triggering a prop types warning.

Will elementType validate the string is a valid DOM element, or can I pass "bologna" without triggering a prop types warning?

It's not clear what elementType is validating, and what's allowable from the Usage section alone.

Can we clarify this?


Consider the following example.

JS Fiddle: https://jsfiddle.net/h02ka7up/63/

function Test() {
    return (
        <Typography component="span">Text</Typography>
    );
}

Typography.propTypes = {
  component: PropTypes.elementType,
  children: PropTypes.node,
};


function Typography(props) {
  	const Component = props.component || "p";
  	return <Component>{props.children}</Component>;
}


ReactDOM.render(
  <Test />,
  document.getElementById('container')
);

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