Skip to content

Ignored static properties of SFCs (Stateless Functional Components) #3

Closed
@mbrowne

Description

@mbrowne

This library respects the use of defaultProps on SFCs, which is good (although in practice you can often just use default argument values for SFCs), but there are other properties an SFC can have that are currently ignored:

  • propTypes
  • contextTypes
  • displayName

I realize that calling a function directly is more efficient than mounting it as a component [1], so skipping displayName makes sense, but IMO propTypes and contextTypes should still be respected, if they exist. So in those cases I would think it would be better to go ahead and render the SFC as a component rather than calling it directly.

Example of PropTypes being ignored:

function Test({ render }) {
    // this should trigger a warning since we're passsing a number and not a string
    return renderProps(render, { name: 123 })
}

function Greeting({ name }) {
    return <div>Hi, {name}</div>
}

Greeting.propTypes = {
    name: PropTypes.string.isRequired
}

export default class App extends Component {
    render() {
        return (
            <div>
                <Test render={Greeting} />
            </div>
        )
    }
}

[1] FWIW, SFCs have new optimizations applied to them in React 16 that make them faster than regular class components ("regular class" meaning React.Component without a shouldComponentUpdate method...as to SFC vs. React.PureComponent, it depends on the situation).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions