new*_*rad 7 twitter-bootstrap typescript reactjs redux
当我从react-bootstrap尝试这个示例代码时,我不断收到错误,例如"参数'上下文'隐含地具有'任意'类型;"类型'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;
Run Code Online (Sandbox Code Playgroud)
在Jumbo.tsx:
const Jumbo = () => (
<FormExample />
);
Run Code Online (Sandbox Code Playgroud)
Har*_*ish 21
在typeScript中你应该安装@ types/react,在扩展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> {... }
Run Code Online (Sandbox Code Playgroud)
希望它会对你有所帮助
Ozg*_*hin 13
在我的例子中,指定构造函数参数的类型解决了这个问题.
class Clock extends React.Component<any, any> {
constructor(props: any) {
super(props);
}
}
Run Code Online (Sandbox Code Playgroud)
我刚刚在功能组件上收到此错误。
为了获取诸如props.children自定义道具之类的信息,您应该执行以下操作。
import { FunctionComponent } from 'react';
const Layout: FunctionComponent<{ hello: string }> = props => (
<div style={layoutStyle}>
<Header />
{props.hello}
{props.children}
</div>
);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14820 次 |
| 最近记录: |