未定义ReactJS未捕获的ReferenceError函数(ReactJS Uncaught ReferenceError function is not defined)

系统教程 行业动态 更新时间:2024-06-14 16:58:29
未定义ReactJS未捕获的ReferenceError函数(ReactJS Uncaught ReferenceError function is not defined)

我正在尝试使用ES6语法在React中实现自定义验证。

import React, { Component } from 'react'; export default class Board extends Component { constructor(props) { super(props); } static propTypes = { count: validate }; validate(props, propName, componentName){ if (props[propName]) { let value = props[propName]; if (typeof value === 'number') { if (value > 100) { return new Error("Value cannot be more than 100"); } } else{ return new Error('Count should be a number') } } }; render() { return ( <div className="board">{this.props.count}</div> ); } }

当我运行此代码时,出现错误“Uncaught ReferenceError:validate not defined”。 如果有人能帮我解决这个问题,我将不胜感激。

I am trying to implement a custom validation in React using ES6 syntax.

import React, { Component } from 'react'; export default class Board extends Component { constructor(props) { super(props); } static propTypes = { count: validate }; validate(props, propName, componentName){ if (props[propName]) { let value = props[propName]; if (typeof value === 'number') { if (value > 100) { return new Error("Value cannot be more than 100"); } } else{ return new Error('Count should be a number') } } }; render() { return ( <div className="board">{this.props.count}</div> ); } }

When I run this code, I get an error "Uncaught ReferenceError: validate is not defined". I will appreciate if someone could help me resolve this.

最满意答案

import React, { Component } from 'react'; export default class Board extends Component { constructor(props) { super(props); } render() { return ( <div className="board">{this.props.count}</div> ); } } const validate = (props, propName, componentName) => { if (props[propName]) { let value = props[propName]; if (typeof value === 'number') { if (value > 100) { return new Error("Value cannot be more than 100"); } } else{ return new Error('Count should be a number') } } }; Board.propTypes = { count: validate }

或者更简单......

import React, { Component } from 'react'; export default class Board extends Component { constructor(props) { super(props); } render() { return ( <div className="board">{this.props.count}</div> ); } } Board.propTypes = { count: (props, propName, componentName) => { if (props[propName]) { let value = props[propName]; if (typeof value === 'number') { if (value > 100) { return new Error("Value cannot be more than 100"); } } else{ return new Error('Count should be a number') } } } } import React, { Component } from 'react'; export default class Board extends Component { constructor(props) { super(props); } render() { return ( <div className="board">{this.props.count}</div> ); } } const validate = (props, propName, componentName) => { if (props[propName]) { let value = props[propName]; if (typeof value === 'number') { if (value > 100) { return new Error("Value cannot be more than 100"); } } else{ return new Error('Count should be a number') } } }; Board.propTypes = { count: validate }

or more simple...

import React, { Component } from 'react'; export default class Board extends Component { constructor(props) { super(props); } render() { return ( <div className="board">{this.props.count}</div> ); } } Board.propTypes = { count: (props, propName, componentName) => { if (props[propName]) { let value = props[propName]; if (typeof value === 'number') { if (value > 100) { return new Error("Value cannot be more than 100"); } } else{ return new Error('Count should be a number') } } } }

更多推荐

本文发布于:2023-04-15 03:18:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/25ed31686f5007afcf33a2453e23f18a.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   未定义   ReactJS   defined   ReferenceError

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!