Skip to content

Add Rule option "disallowedFor" for component prop #3422

Closed
@jacketwpbb

Description

@jacketwpbb

Suppose we have a component that accept a prop named string.

function Comp(props) {
    return <div>{props.string}</div>;
}

function App() {
    return <Comp string="213"  />;
}

As needs change, we need to replace string prop by another named number.

function Comp(props) {
    return <div>{props.number || props.string}</div>;
}
Comp.propTypes = {
    number: PropTypes.number,
    /**
     * @deprecated use number instead
     */
    string: PropTypes.string
};
Comp.defaultProps = {
    string: '123',
    number: 123
};

The string prop will works in a period of time, but it's better to replace it with number prop.
This rule option can be use to avoid using deprecated prop for Comp.

{
"react/forbid-component-props": [
    "error", { "forbid": [{"propName": "string", "disallowedList": ["Comp"]}] }
  ]
}

image

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