react PropTypes校驗傳遞的值操作示例
更新時間:2020年04月28日 08:43:20 作者:人生如初見_張默
這篇文章主要介紹了react PropTypes校驗傳遞的值操作,結(jié)合實例形式分析了react PropTypes針對傳遞的值進行校驗操作相關(guān)實現(xiàn)技巧,需要的朋友可以參考下
本文實例講述了react PropTypes校驗傳遞的值操作。分享給大家供大家參考,具體如下:
校驗傳遞的值:
import React, { Component, Fragment } from 'react';
import List from './List.js';
class Test extends Component {
constructor(props) {
super(props);
this.state={
inputValue:'aaa',
list:['睡覺','打游戲'],
}
// this.add=this.add.bind(this);
}
addList() {
this.setState({
list:[...this.state.list,this.state.inputValue],
inputValue:''
})
}
change(e) {
this.setState({
inputValue:e.target.value
})
}
delete(i) {
// console.log(i);
const list = this.state.list;
list.splice(i,1);
this.setState({
list:list
})
}
render() {
return (
<Fragment>
<div>
<input value={this.state.inputValue} onChange={this.change.bind(this)}/>
<button onClick={this.addList.bind(this)}>添加</button>
</div>
<ul>
{
this.state.list.map((v,i)=>{
return(
<List key={i} content={v} index={i} delete={this.delete.bind(this)} />
);
})
}
</ul>
</Fragment>
);
}
}
export default Test;
import React, { Component } from 'react';
import PropTypes from 'prop-types';
class List extends Component {
constructor(props) {
super(props);
this.delete = this.delete.bind(this);
}
render() {
return (
<li
onClick={this.delete}
>{this.props.name}{this.props.content}</li>
);
}
delete=() => {
this.props.delete(this.props.index);
}
}
//傳值校驗
List.propTypes={
name:PropTypes.string.isRequired,
content:PropTypes.string,
index:PropTypes.number,
delete:PropTypes.func
}
//設(shè)置默認值:
List.defaultProps={
name:'張三'
}
export default List;
希望本文所述對大家react程序設(shè)計有所幫助。
您可能感興趣的文章:
相關(guān)文章
React-Native左右聯(lián)動List的示例代碼
本篇文章主要介紹了React-Native左右聯(lián)動List的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-09-09
React?Fiber?樹思想解決業(yè)務(wù)實際場景詳解
這篇文章主要為大家介紹了React?Fiber?樹思想解決業(yè)務(wù)實際場景詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-12-12
React組件中使用JSON數(shù)據(jù)文件的方法詳解
要在 React 組件中使用 JSON 數(shù)據(jù),有多種方法,這篇文章主要為大家詳細介紹了五個常見的方法,文中的示例代碼講解詳細,有需要的小伙伴可以了解下2024-01-01
解析TypeError:import_react_native.AppState.removeEventListener
這篇文章主要為大家介紹了TypeError:import_react_native.AppState.removeEventListener?is?not?a?function問題解決分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-09-09

