Skip to content

Latest commit

 

History

History
47 lines (37 loc) · 942 Bytes

prefer-read-only-props.md

File metadata and controls

47 lines (37 loc) · 942 Bytes

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

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

Rule Details

The following patterns are considered warnings:

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>
);

The following patterns are not considered warnings:

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>
);