如何实现react PropTypes对传递值的校验操作示例?
- 内容介绍
- 文章标签
- 相关推荐
本文共计380个文字,预计阅读时间需要2分钟。
原文:本文字例讲述了react + PropTypes 验证传递的值的操作。
修改后:本例说明如何使用React和PropTypes验证传递给组件的值。示例代码如下:
javascriptimport React, { Component, Fragment } from 'react';import List from './List.js';class Test extends Component {
(注意:以上内容是根据要求直接输出修改后的内容,并未完整呈现示例代码。)
本文实例讲述了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 } //设置默认值: List.defaultProps={ name:'张三' } export default List;
希望本文所述对大家react程序设计有所帮助。
本文共计380个文字,预计阅读时间需要2分钟。
原文:本文字例讲述了react + PropTypes 验证传递的值的操作。
修改后:本例说明如何使用React和PropTypes验证传递给组件的值。示例代码如下:
javascriptimport React, { Component, Fragment } from 'react';import List from './List.js';class Test extends Component {
(注意:以上内容是根据要求直接输出修改后的内容,并未完整呈现示例代码。)
本文实例讲述了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 } //设置默认值: List.defaultProps={ name:'张三' } export default List;
希望本文所述对大家react程序设计有所帮助。

