Skip to content

Latest commit

 

History

History
51 lines (39 loc) · 1.09 KB

prefer-read-only-props.md

File metadata and controls

51 lines (39 loc) · 1.09 KB

Enforce that props are read-only (react/prefer-read-only-props)

🔧 This rule is automatically fixable by the --fix CLI option.

Using Flow, one can define types for props. This rule enforces that prop types are read-only (covariant).

Rule Details

Examples of incorrect code for this rule:

type Props = {
  name: string,
}
class Hello extends React.Component<Props> {
  render () {
    return <div>Hello {this.props.name}</div>;
  }
}

function Hello(props: {-name: string}) {
  return <div>Hello {props.name}</div>;
}

const Hello = (props: {|name: string|}) => (
  <div>Hello {props.name}</div>
);

Examples of correct code for this rule:

type Props = {
  +name: string,
}
class Hello extends React.Component<Props> {
  render () {
    return <div>Hello {this.props.name}</div>;
  }
}

function Hello(props: {+name: string}) {
  return <div>Hello {props.name}</div>;
}

const Hello = (props: {|+name: string|}) => (
  <div>Hello {props.name}</div>
);