React进阶 - 11( 说一说 PropTypes 和 DefaultProps )

本章内容

目录

截止到上一节的内容,我们使用了一个 TodoList的案例,大概了解了 React的一些入门知识。从本节内容开始,我们将进入React进阶知识的学习

PropTypes

  • 在组件拆分时,我们知道每个组件都有自己的 props,这个参数是从父组件那里接收的一些数据。

  • 那么有个疑问,子组件在接受参数的时候,怎么对这些参数"类型"做校验呢,又如何定义参数的"默认值"呢?

  • 打开之前工程里的 TodoItem.js组件,可以看到其父组件传过来的参数都有属于自己的"类型"

  • React可以使用 PropTypes对传递的属性进行"类型"强校验

js 复制代码
import React, { Component } from 'react'
import PropTypes from 'prop-types' // 1、引入 PropTypes


class TodoItem extends Component {
  constructor(props) {
    super(props)

    this.handleClick = this.handleClick.bind(this)
  }
  render () {
    const { content } = this.props
    return (
      <div onClick={this.handleClick}>
        {content}
      </div>
    )
  }

  handleClick() {
    const { deleteFn, index } = this.props
    deleteFn(index)
  }
}

// 2、使用 PropTypes 对属性进行强校验
TodoItem.propTypes = {
  content: PropTypes.string,
  index: PropTypes.number,
  deleteFn: PropTypes.func
}

export default TodoItem
  • 运行代码后,发现正常运行,没有报错。

  • 注意的是,如果你在子组件将 index的类型设置为 string, 界面将会出现一个"警告",因为父组件那边传递的类型是 number类型,跟设定的不符

js 复制代码
import React, { Component } from 'react'
import PropTypes from 'prop-types' // 1、引入 PropTypes


class TodoItem extends Component {
  constructor(props) {
    super(props)

    this.handleClick = this.handleClick.bind(this)
  }
  render () {
    const { content } = this.props
    return (
      <div onClick={this.handleClick}>
        {content}
      </div>
    )
  }

  handleClick() {
    const { deleteFn, index } = this.props
    deleteFn(index)
  }
}

// 2、使用 PropTypes 对属性进行强校验
TodoItem.propTypes = {
  content: PropTypes.string,
  index: PropTypes.string, // 如果这里设置为 string,那么界面会出现警告
  deleteFn: PropTypes.func
}

export default TodoItem

DefaultProps

  • 接着上面的代码,现在有个新需求: 假设在子组件中,子组件必须要求父组件给它传递一个"属性"(例如:title), 但是父组件由于某些原因,不能传递这个属性,此时应该怎么解决呢?
js 复制代码
import React, { Component } from 'react'
import PropTypes from 'prop-types' 

class TodoItem extends Component {
  constructor(props) {
    super(props)

    this.handleClick = this.handleClick.bind(this)
  }
  render () {
    // 1、子组件要求父组件给它传递一个 title 属性
    const { content, title } = this.props
    return (
      <div onClick={this.handleClick}>
        {/* 2、并且将 title 属性显示在界面上 */}
        {title}---{content}
      </div>
    )
  }

  handleClick() {
    const { deleteFn, index } = this.props
    deleteFn(index)
  }
}

TodoItem.propTypes = {
  title: PropTypes.string.isRequired, // 3、要求父组件必须传递一个 title 属性
  content: PropTypes.string,
  index: PropTypes.number,
  deleteFn: PropTypes.func
}

export default TodoItem
  • 运行一下界面,发现控制台出现了一个警告(因为父组件确实没有传递这个 title属性,但是子组件他又必须要)。

  • 要解决上面的警告,那使用 defaultPropstitle属性设置一个"默认值"

js 复制代码
import React, { Component } from 'react'
import PropTypes from 'prop-types' 

class TodoItem extends Component {
  constructor(props) {
    super(props)

    this.handleClick = this.handleClick.bind(this)
  }
  render () {
    const { content, title } = this.props
    return (
      <div onClick={this.handleClick}>
        {title}---{content}
      </div>
    )
  }

  handleClick() {
    const { deleteFn, index } = this.props
    deleteFn(index)
  }
}

TodoItem.propTypes = {
  title: PropTypes.string.isRequired,
  content: PropTypes.string,
  index: PropTypes.number,
  deleteFn: PropTypes.func
}

TodoItem.defaultProps = {
  title: '我是子组件' // 给 title 设置一个"默认值"
}

export default TodoItem
  • 设置完"默认值"后,再次运行界面,发现一切正常

到此,本章内容结束!

相关推荐
扶苏10021 天前
vue使用event.dataTransfer实现A容器数据拖拽复制到到B容器
前端·vue.js·chrome
David凉宸1 天前
Vue 3 项目的性能优化策略:从原理到实践
前端·vue.js·性能优化
小马_xiaoen1 天前
Proxy 与 Reflect 从入门到实战:ES6 元编程核心特性详解
前端·javascript·ecmascript·es6
hoiii1871 天前
MATLAB SGM(半全局匹配)算法实现
前端·算法·matlab
飞羽殇情1 天前
基于React Native鸿蒙跨平台开发构建完整电商预售系统数据模型,完成参与预售、支付尾款、商品信息展示等
react native·react.js·华为·harmonyos
摘星编程1 天前
React Native + OpenHarmony:ImageSVG图片渲染
javascript·react native·react.js
会编程的土豆1 天前
新手前端小细节
前端·css·html·项目
摘星编程1 天前
OpenHarmony + RN:Text文本书写模式
javascript·react native·react.js
广州华水科技1 天前
单北斗GNSS在桥梁形变监测中的应用与技术进展分析
前端
我讲个笑话你可别哭啊1 天前
鸿蒙ArkTS快速入门
前端·ts·arkts·鸿蒙·方舟开发框架