2023-05-03 11:06:20 +02:00
== Why is this an issue?
2021-06-08 14:23:48 +02:00
If the type of a ``++props++`` property is not defined there is a great chance one will wrongly use the value returned by the property and generate a not expected behavior of the application.
2023-05-03 11:06:20 +02:00
=== Noncompliant code example
2021-06-08 14:23:48 +02:00
2022-02-04 17:28:24 +01:00
[source,javascript]
2021-06-08 14:23:48 +02:00
----
class Rating extends React.Component {
render() {
return ( <div>{this.props.rating}</div> );
}
}
----
2023-05-03 11:06:20 +02:00
=== Compliant solution
2021-06-08 14:23:48 +02:00
2022-02-04 17:28:24 +01:00
[source,javascript]
2021-06-08 14:23:48 +02:00
----
import PropTypes from 'prop-types';
class Rating extends React.Component {
render() {
return ( <div>{this.props.rating}</div> );
}
}
Rating.propTypes = {
rating: PropTypes.number.isRequired
};
----
ifdef::env-github,rspecator-view[]
2021-06-08 15:52:13 +02:00
'''
2021-06-08 14:23:48 +02:00
== Comments And Links
(visible only on this page)
2023-05-25 14:18:12 +02:00
=== on 23 Feb 2018, 11:57:13 Alexandre Gigleux wrote:
Covered by ESLint for Reach: \https://github.com/yannickcr/eslint-plugin-react/blob/HEAD/docs/rules/prop-types.md (react/prop-types)
2021-06-08 14:23:48 +02:00
endif::env-github,rspecator-view[]