React propTypes for'系统提供的'props

时间:2018-09-29 16:41:29

标签: reactjs react-proptypes

关于某些React组件提供的道具的PropType最佳实践是什么,例如:

  • props.location和props.match提供者by ReactRouter
  • props.dispatch(),以防组件“连接”到Redux存储

是否需要对这些属性使用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)

1 个答案:

答案 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
};