Omit the value of the prop when it is explicitly . eslint:
// bad
<Foo
hidden={true}
/>
// good
<Foo
hidden
/>
// good
<Foo hidden />
Always include an
alt
prop on<img>
tags. If the image is presentational,alt
can be an empty string or the<img>
must haverole="presentation"
. eslint:jsx-a11y/alt-text
// bad
<img src="hello.jpg" />
// good
<img src="hello.jpg" alt="Me waving hello" />
// good
<img src="hello.jpg" alt="" />
// good
<img src="hello.jpg" role="presentation" />
Do not use words like “image”, “photo”, or “picture” in
<img>
props. eslint:Use only valid, non-abstract ARIA roles. eslint:
// bad - not an ARIA role
<div role="datepicker" />
<div role="range" />
// good
<div role="button" />
-
// bad
<div accessKey="h" />
// good
<div />
Avoid using an array index as
key
prop, prefer a stable ID.
We don’t recommend using indexes for keys if the order of items may change.
Always define explicit defaultProps for all non-required props.
// bad
function SFC({ foo, bar, children }) {
return <div>{foo}{bar}{children}</div>;
}
SFC.propTypes = {
foo: PropTypes.number.isRequired,
bar: PropTypes.string,
children: PropTypes.node,
};
// good
function SFC({ foo, bar, children }) {
}
SFC.propTypes = {
foo: PropTypes.number.isRequired,
bar: PropTypes.string,
children: PropTypes.node,
SFC.defaultProps = {
bar: '',
children: null,
};
Use spread props sparingly.
Exceptions:
HOCs that proxy down props and hoist propTypes
function HOC(WrappedComponent) {
return class Proxy extends React.Component {
Proxy.propTypes = {
text: PropTypes.string,
isLoading: PropTypes.bool
};
render() {
return <WrappedComponent {...this.props} />
}
}
}
Spreading objects with known, explicit props. This can be particularly useful when testing React components with Mocha’s beforeEach construct.
// bad
render() {
const { irrelevantProp, ...relevantProps } = this.props;
return <WrappedComponent {...this.props} />
}
// good
render() {
const { irrelevantProp, ...relevantProps } = this.props;
}