关于某些React组件提供的道具的PropType最佳实践是什么,例如:
是否需要对这些属性使用propTypes?
在props.match的情况下,我应该检查属性多深? 如果仅在特定组件中需要match.url,是否足以检查props.match还是应该检查props.match.url?
谢谢
import PropTypes from 'prop-types'
class ProductsList extends Component {
constructor(props) {
super(props);
this.handleSelect = this.handleSelect.bind(this);
}
handleSelect(product_id) {
this.props.dispatch(handleAddProduct(product_id));
}
render() {
const {match, location} = this.props;
return (... JSX code goes here ....)
}
}
ProductsList.propTypes = {
match: PropTypes.object.isRequired,
location: PropTypes.object.isRequired
dispatch: PropTypes.func.isRequired
}
export default connect()(ProductsList)
答案 0 :(得分:0)
不。库本身使用propTypes。检查this链接
Route.propTypes = {
children: PropTypes.oneOfType([PropTypes.func, PropTypes.node]),
component: PropTypes.func,
exact: PropTypes.bool,
location: PropTypes.object,
path: PropTypes.string,
render: PropTypes.func,
sensitive: PropTypes.bool,
strict: PropTypes.bool
};