React传值,验证值的类型和默认值

const ele = <Ff const={'哈哈'} index={55}></Ff>
let box = document.querySelector('#app')
// console.log(box)
// 挂载
render(ele,box)

  

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

export default  class ClickS extends React.Component {
  constructor (props) {
    super(props)
    this.state= {
      msg: '123'
    }
    console.log(this.props)
  }
  render () {
    return <div>
  <h2>{this.state.msg}------{this.props.index}---{this.props.const}---{this.props.name}</h2>
    </div>
  }

ClickS.propTypes = {
  const: PropTypes.string.isRequired,  // 表示必须传递,其类型为string
 index: PropTypes.number
}

  // func 表示验证的为一个方法

---------------------------------------------------------------------------------------------------------------------------------------------------------------------

 如果没有传递值,给其一个默认值

ClickS.defaultProps = {
  name: '张三'
}

  

猜你喜欢

转载自www.cnblogs.com/js-liqian/p/11829071.html
今日推荐