zoukankan      html  css  js  c++  java
  • 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: '张三'
    }
    

      

  • 相关阅读:
    Vivian's Problem UVA
    Prime k-tuple UVA
    M
    Make a Crystal UVA
    Cryptography Reloaded UVALive
    Gauss Prime UVA
    Discrete Square Roots UVALive
    The Bells are Ringing UVALive
    Huge Mods UVA
    指数循环节
  • 原文地址:https://www.cnblogs.com/js-liqian/p/11829071.html
Copyright © 2011-2022 走看看