最佳答案
我有以下组成部分(radioOther.jsx
) :
'use strict';
//module.exports = <-- omitted in update
class RadioOther extends React.Component {
// omitted in update
// getInitialState() {
// propTypes: {
// name: React.PropTypes.string.isRequired
// }
// return {
// otherChecked: false
// }
// }
componentDidUpdate(prevProps, prevState) {
var otherRadBtn = this.refs.otherRadBtn.getDOMNode();
if (prevState.otherChecked !== otherRadBtn.checked) {
console.log('Other radio btn clicked.')
this.setState({
otherChecked: otherRadBtn.checked,
});
}
}
onRadChange(e) {
var input = e.target;
this.setState({
otherChecked: input.checked
});
}
render() {
return (
<div>
<p className="form-group radio">
<label>
<input type="radio"
ref="otherRadBtn"
onChange={this.onRadChange}
name={this.props.name}
value="other"/>
Other
</label>
{this.state.otherChecked ?
(<label className="form-inline">
Please Specify:
<input
placeholder="Please Specify"
type="text"
name="referrer_other"
/>
</label>)
:
('')
}
</p>
</div>
)
}
};
在使用 ECMAScript6之前一切正常,现在我得到了1个错误,1个警告,我还有一个后续问题:
错误: Uncatch TypeError: 无法读取 null 的属性‘ other Checked’
警告: getInitialState 是在 RadioOther 上定义的,这是一个普通的 JavaScript 类 你的意思是定义一个状态属性吗?
Can anyone see where the error lies, I know it is due to the conditional statement in the DOM but apparently I am not declaring its initial value correctly?
我应该让 GetInitialState静止吗
如果 getInitialState 不正确,声明原型的合适位置在哪里?
更新:
RadioOther.propTypes = {
name: React.PropTypes.string,
other: React.PropTypes.bool,
options: React.PropTypes.array }
module.exports = RadioOther;
@ssorallen, this code :
constructor(props) {
this.state = {
otherChecked: false,
};
}
产生 "Uncaught ReferenceError: this is not defined"
,并在下面修正
constructor(props) {
super(props);
this.state = {
otherChecked: false,
};
}
but now, clicking the other button now produces error:
Uncaught TypeError: Cannot read property 'props' of undefined