【发布时间】:2016-01-12 07:59:54
【问题描述】:
我正在尝试使用 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>
);
}
}
当我运行此代码时,我收到一个错误“未捕获的 ReferenceError:验证未定义”。如果有人能帮我解决这个问题,我将不胜感激。
【问题讨论】:
-
propTypes是static,因此不应访问 instance 方法。要么将validate设为静态,要么将propTypes设为实例变量
标签: reactjs ecmascript-6