(十)React propTypes defaultProps

- parent component subassembly to pass values:

  • -defaultProps: Sons components by value, if invoked when the parent component sub-assemblies sub-assemblies not to pass value, you can use the default value defaultProps defined in the sub-assembly
  • -propTypes: verify the legitimacy of the parent component passed value type

- The above two properties are defined in 子组件the


  • -1 defaultProps, default attributes, the attribute value is not passed when calling parent component subassembly to the subassembly, and
    needs to display the default value of the sub-assembly,
//具体用法,需要在子组件中

Header.defaultProps={
    title:'标题'
}
  • -2 propTypes type defined parameters, and the legality
/*
    1 引入import PropTypes from 'prop-types'
    2 类.propTypes = {};
*/

import PropTypes from 'prop-types';
class Greeting extends React.Compinent{
    render(){
        return(<h1>好的</h1>);
    }
    
    Greeting.propTypes = {
        name: PropTypes.string
    }
}
Published 106 original articles · won praise 10 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_32060101/article/details/101452021