首页 > 其他 > 详细

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

时间:2019-11-10 11:23:36      阅读:75      评论:0      收藏:0      [点我收藏+]
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: ‘张三‘
}

  

技术分享图片

 

 

技术分享图片

 

 

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

原文:https://www.cnblogs.com/js-liqian/p/11829071.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!