React - defaultProps vs ES6 default params when destructuring (performances issues)

前端 未结 3 822
遥遥无期
遥遥无期 2020-12-13 00:16

I just came across a question about React performances when settings default values in one of my stateless functional components.

This component had

3条回答
  •  遥遥无期
    2020-12-13 00:39

    One big difference between defaultProps and default function parameters is that the former will be checked against propTypes. The require-default-props rule of eslint-plugin-react explains it very well.

    One advantage of defaultProps over custom default logic in your code is that defaultProps are resolved by React before the PropTypes typechecking happens, so typechecking will also apply to your defaultProps. The same also holds true for stateless functional components: default function parameters do not behave the same as defaultProps and thus using defaultProps is still preferred.

提交回复
热议问题