自带的属性验证

 import React, { Component } from 'react'
import PropTypes from 'prop-types';

 function ChildCpn(props){ 
      // console.log(props);  // {name: "Eric", age: "28"} 
    let {name,age} = props;
    const {names} = props;
    return (
      <div>
      <h2>子组件展示数据: {name}-{age}</h2> 

      <ul>
          {
            names.map((item,index)=>{
              return <li key={index}>{item}</li>
            })
          }
      </ul>
      </div>
      
    ) 
 }

 ChildCpn.propTypes = { 
   name: PropTypes.string,
   age:PropTypes.number,
   names:PropTypes.array
 }

 // 默认值
 ChildCpn.defaultProps = {
   name:'AAA',
   age:30,
   names:[23,45,67]
 }
   
 export default class App extends Component {  
   render() { 
     return (
       <div> 
          <ChildCpn name="Eric" age={28} names = {[1,2,3]}/> 
          <ChildCpn /> 
       </div>
     )
   } 
 }
 
posted @ 2021-08-10 17:56  13522679763-任国强  阅读(21)  评论(0)    收藏  举报