8. react 基础 - props 默认值和类型限制 与 Props , State , render 函数 关系
一. PropTypes 与 DefaultProps 官方文档
1. PropTypes 属性校验
引入 PropTypes
import PropTypes from 'prop-types';
强校验 props 属性
eg:
import React, { Component } from 'react'
import PropTypes from 'prop-types'
class TodoItem extends Component{
constructor(props){
super(props);
}
}
// 校验 传递过来的 value 为 string 类型
// 校验 传递过来的 itemDelete 为 function 类型
// 校验 传递过来的 index 为 string 类型 并且必须要传递
// 如果传递的数据不对 会在 控制太报一个警告
TodoItem.propTypes = {
value: PropTypes.string,
itemDelete: PropTypes.func,
index: PropTypes.string.isRequired
}
export default TodoItem;
2.DefaultProps 设置默认值
eg:
import React, { Component } from 'react'
class TodoItem extends Component{
constructor(props){
super(props);
}
}
// 设置 props 的 test默认属性为 hello world
TodoItem.defaultProps = {
test: 'hello world'
}
export default TodoItem;
二. Props , State , render 函数 关系
// 当组件 的 props 和 state 发生改变时 render 方法会重新执行
// 当父组件 的 render 函数被运行时, 子组件 的 render 都会被运行
8. react 基础 - props 默认值和类型限制 与 Props , State , render 函数 关系的更多相关文章
- Swift语言指南(七)--语言基础之布尔值和类型别名
原文:Swift语言指南(七)--语言基础之布尔值和类型别名 布尔值 Swift有一个基本布尔类型,叫做布尔(bool),布尔值又称逻辑值(logical),因为它只能为真(true)或假(false ...
- React传值,验证值的类型和默认值
const ele = <Ff const={'哈哈'} index={55}></Ff> let box = document.querySelector('#app') / ...
- 十、React 父组件传来值的类型控制propTypes、父组件如果不传值defaultProps
父组件给子组件传值时: 1.defaultProps:父子组件传值中,如果父组件调用子组件的时候不给子组件传值,可以在子组件中使用defaultProps定义的默认值: 2.propTypes:验证父 ...
- react input 设置默认值
1.text类型 <input type="text" value={默认值} /> ,这种写法可以显示默认值,但不能对输入框进行编辑 正确写法: <input ...
- react中属性默认值是true?
看到项目代码中 return ( <MyWebView state="Login" title="登录app" ref="login" ...
- php基础--取默认值以及类的继承
(1)对于php的默认值的使用和C++有点类似,都是在函数的输入中填写默认值,以下是php方法中对于默认值的应用: <?phpfunction makecoffee($types = array ...
- JS基础_返回值的类型
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- vue props默认值国际化报错
未做国际化处理 tabLabel: { type: Array, default: () => (["a", "b", "c"]) } ...
- ES6 函数参数的默认值
基本用法 在ES6之前,不能直接为函数的参数指定默认值,只能采取变通的方法. function log(x,y){ y = y||'world'; console.log(x,y); } log('k ...
随机推荐
- springmvc 后台实体类接受前端json字符串时,其中一个属性content 接受富文本内容时 标签<p>、<span> 这些标签丢失问题解决
问题描述: 前端一个字段 <script id="editor" type="text/plain" name="content" s ...
- 我的Grunt之旅-初识gruntfile文件
时间:2018-03-06 18:23 事件:配置 gruntfile.js文件 首先,回忆一下之前的点,grunt项目下面必须有两个文件 ,第一个 package.json ,第二个 Gru ...
- Day8 - C - Another Problem on Strings CodeForces - 165C
A string is binary, if it consists only of characters "0" and "1". String v is a ...
- angularJS MVC及$scope作用域
- oracle job不运行,定位问题
一. job的运行频率设置 1.每天固定时间运行,比如早上8:10分钟:Trunc(Sysdate+1) + (8*60+10)/24*60 2.Toad中提供的: 每天:trunc(sysdate+ ...
- Django(二) 模板:基本使用、模板语法、模板继承
一.模板的使用实战 接:https://blog.csdn.net/u010132177/article/details/103788677 参考:https://docs.djangoproject ...
- linux 查看运行进程详细信息
Linux在启动一个进程时,系统会在/proc下创建一个以PID命名的文件夹,在该文件夹下会有我们的进程的信息 通过ll或ls –l命令即可查看. ll /proc/PID cwd符号链接的是进程运行 ...
- Spark技术学院-进去能学到啥?
Spark技术学院是什么? 主要是浪尖,前腾讯现阿里的大神一起搞的知识分享基地,旨在帮助大家由入门到精通spark,hbase,kafka大数据重要的框架,还有给入门小白指点入门方法,分享入门资料,对 ...
- MSDN原版系统镜像ISO下载站
官网 网址1:http://www.imsdn.cn/ Windows 10 最新版本 1909 正式版 ISO 镜像下载 (微软 MSDN / VL 官方原版系统) 网址2:https://www. ...
- oracle和mysql的一些区别
1.分页查询语句的区别 2.字符串拼接的区别,oracle不支持三个字符串的拼接,mysql支持三个字符串的拼接,在mybatis中,连接的是oracle数据库,字符串的拼接需要如下语句: <s ...