Skip to content

react/forbid-foreign-prop-types warning for static reference to current class #2023

Closed
@ehaynes99

Description

@ehaynes99

Referencing the propTypes object of the current class causes this warning:

Using propTypes from another component is not safe because they may be removed in production builds

I've used this pattern in numerous places to effectively say, "pass all props but those I handle":

import React from 'react'
import { object, string } from 'prop-types'
import { omit } from 'lodash'

class Wrapper extends React.Component {
  static propTypes = {
    className: string,
    style: string,
  }
  
  render() {
    // pass everything except className and title to child component
    const childProps = omit(this.props, Object.keys(Wrapper.propTypes))
    return (
      <div className={this.props.className} style={this.props.style}>
        <div {...childProps}>
          Inner content
        </div>
      </div>
    )
  }
}

Is this a false positive, or is the reason for this warning still applicable when the "other" component is actually the same component? I imagine it comes down to whether Webpack/babel plugins strip it blindly, or somehow creates a closure over it if referenced within the same class. If the latter, perhaps the message could be tweaked a bit to suggest that any static reference is not safe.

I know I can use rest/spread to filter the list, but when the list is large, I've found this to be a nice alternative to a bunch of repetition.

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