当我从react- bootstrap尝试此示例代码时,我不断收到错误消息,例如“参数’context’隐式具有’any’类型;“属性’value’在’Readonly <{}>类型上不存在”。
在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 /> );
在 typeScript中, 您应该安装@ types / react,并在扩展React.Component时需要指定propsand state类型。这是例子
React.Component
props
state
import * as React from 'react' interface Props { ... // your props validation } interface State { ... // state types } class FormExample extends React.Component<Props, State> {... }