and*_*son 8 javascript jsx reactjs
我正在使用反应js,我不知道为什么我没有定义道具.
这是我的课.
import React, { Component } from 'react';
const InputHeight = {
height: '50px',
}
function clearData() {
this.refs.input.value = "";
}
export default class TextInput extends Component {
render() {
return (
<input
className="form-control"
ref="input"
name={props.name}
type={props.inputType}
value={props.content}
pattern={props.pattern}
onChange={props.controlFunc}
placeholder={props.placeholder}
style={InputHeight}
required />
);
}
}
TextInput.propTypes = {
inputType: React.PropTypes.oneOf(['text', 'number', 'email']).isRequired,
name: React.PropTypes.string.isRequired,
controlFunc: React.PropTypes.func.isRequired,
content: React.PropTypes.oneOfType([
React.PropTypes.string,
React.PropTypes.number,
]).isRequired,
placeholder: React.PropTypes.string,
};
Run Code Online (Sandbox Code Playgroud)
编译失败./src/components/Parts/SmallBits/FormItems/TextInput.js第19行:'道具'未定义no-undef第20行:'道具'未定义no-undef第21行:'道具'不是定义no-undef第22行:'props'未定义no-undef第23行:'props'未定义no-undef第24行:'props'未定义no-undef
搜索关键字以了解有关每个错误的更多信息.
this.refs.form.clearData();
Run Code Online (Sandbox Code Playgroud)
只是点击它,它给了我
未捕获的TypeError:无法读取null的属性'refs'
Cha*_*man 26
在课堂上,访问道具的方式this.props不仅仅是props.
export default class TextInput extends Component {
render() {
return (
<input
className="form-control"
ref="input"
name={this.props.name}
type={this.props.inputType}
value={this.props.content}
pattern={this.props.pattern}
onChange={this.props.controlFunc}
placeholder={this.props.placeholder}
style={InputHeight}
required />
);
}
}
Run Code Online (Sandbox Code Playgroud)
以下是使用此更改修改的代码.
至于这个功能
function clearData() {
this.refs.input.value = "";
}
Run Code Online (Sandbox Code Playgroud)
我相信你有2个问题.首先,它没有嵌套在类中,因此this关键字不是指这个类.其次,即使它是嵌套的,一旦调用者调用此函数,this关键字的上下文现在将不再引用您的类.了解this关键字的工作原理以及如何使用bind或=>函数来解决此问题非常重要.
| 归档时间: |
|
| 查看次数: |
16044 次 |
| 最近记录: |