When I try this sample code from react-bootstrap, I keep getting errors such as " Parameter 'context' implicitly has an 'any' type; "Property 'value' does not exist on type 'Readonly<{}>'."
in 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;
in Jumbo.tsx:
const Jumbo = () => (
<FormExample />
);
The React. js error "Property does not exist on type 'Readonly<{}>'" occurs when we try to access the props or state of a class component which we haven't typed. To solve the error, use the generic on the React. Component class to type the props or state objects of the class.
To update your React version, install the latest versions of the react and react-dom packages by running npm install react@latest react-dom@latest . If you use create-react-app , also update the version of react-scripts . Copied! The command will update the versions of the react-related packages.
In typeScript you should install @types/react and while extending the React.Component
you need to specify the props
and state
types.
Here is the example
import * as React from 'react'
interface Props {
... // your props validation
}
interface State {
... // state types
}
class FormExample extends React.Component<Props, State> {... }
Specifying the type of the constructor parameter resolved this issue in my case.
class Clock extends React.Component<any, any> {
constructor(props: any) {
super(props);
}
}
I just got this error on a functional component.
In order to get information such as props.children
as well as custom props, you should do the following.
import { FunctionComponent } from 'react';
const Layout: FunctionComponent<{ hello: string }> = props => (
<div style={layoutStyle}>
<Header />
{props.hello}
{props.children}
</div>
);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With