当我从 react-bootstrap 尝试此示例代码时,我不断收到错误消息,例如“参数‘上下文’隐含地具有‘任何’类型;“属性‘值’在类型‘只读<{}>’上不存在。”
在 form.tsx 中:
class FormExample extends React.Component {
constructor(props, context) {
super(props, context);
this.handleChange = this.handleChange.bind(this);
this.state = {
value: ''
};
}
getValidationState() {
const length = this.state.value.length;
if (length > 10) return 'success';
else if (length > 5) return 'warning';
else if (length > 0) return 'error';
return null;
}
handleChange(e) {
this.setState({ value: e.target.value });
}
render() {
return (
<form>
<FormGroup
controlId="formBasicText"
validationState={this.getValidationState()}
>
<ControlLabel>Working example with validation</ControlLabel>
<FormControl
type="text"
value={this.state.value}
placeholder="Enter text"
onChange={this.handleChange}
/>
<FormControl.Feedback />
<HelpBlock>Validation is based on string length.</HelpBlock>
</FormGroup>
</form>
);
}
}
export default FormExample;
在 Jumbo.tsx 中:
const Jumbo = () => (
<FormExample />
);
原文由 newgrad 发布,翻译遵循 CC BY-SA 4.0 许可协议
在 _typeScript 中_,您应该安装 @types/react 并在扩展
React.Component
时需要指定props
和state
类型。这是示例