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: '张三'
    }
    

      

  • 相关阅读:
    使用vue-cli脚手架搭建简单项目框架
    总结HTML5
    导出网页内容
    vue 编写插件
    各ui库项目结构
    webpack 模块方法
    webpack4.0
    webapp优化
    http/2.0与http/1.1的区别
    setTimeout与Promise的区别
  • 原文地址:https://www.cnblogs.com/js-liqian/p/11829071.html
Copyright © 2011-2022 走看看