I am learning how to develop applications using React as front-end and spring as back-end. While developing a sample application, I encountered an error as follows:
`(26,28): Property "value" does not exist on type 'Readonly<{}>`
This error is generated from my App.tsx file which contains the React Components definition in JSX. The class component with the state "value" is defined as follows.
class App extends React.component{
constructor(props:any) {
super(props);
this.state = {
value:[],
isLoading:false
};
}
...
25 render(){
26 const value = this.state.value;
27 const isLoading = this.state.isLoading;
...
}
}//End of Class*
I am not understanding what's wrong. Can someone please help me look at it in a different perspective so that this problem can be approached in a different manner?
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. Here is an example of how the error occurs.
State is the local state of the component which cannot be accessed and modified outside of the component. It's equivalent to local variables in a function. Props, on the other hand, make components reusable by giving components the ability to receive data from their parent component in the form of props.
To change a value in the state object, use the this. setState() method. When a value in the state object changes, the component will re-render, meaning that the output will change according to the new value(s).
To use state in functional component use useState hook. This hook returns a tuple of two values. The first one is the current value of the state, the second one is a function to update the state. // into useState as an argument.
To fix the “Property ‘value’ does not exist on type ‘Readonly< {}>’ ” error with React TypeScript, we should specify the prop and state types in our React class component. type MyProps = { //... }; type MyState = { value: string }; class App extends React.Component<MyProps, MyState> { //...
The React.js error "Property 'value' does not exist on type 'HTMLElement'" occurs when we try to access the value property on an element that has a type of HTMLElement. To solve the error, use a type assertion to type the element as HTMLInputElement before accessing the property. Here is an example of how the error occurs. Copied!
Property 'value' does not exist on type 'EventTarget & Element'. ts2339: property 'passwordicon' does not exist on type 'readonly< {}>'. react modal property 'showmodal' does not exist on type 'readonly< {}>'. Cannot inline bytecode built with JVM target 1.8 into bytecode that is being built with JVM target 1.6.
As for the issue of data not being persisted, the local component state does not store any data beyond its lifecycle. You can either save the data in your database, or cache it in your local storage. Thanks for contributing an answer to Stack Overflow!
Did you declare an interface for your state
?
Take a look at Hello React and TypeScript which shows an example of using interfaces with React Components.
I suspect you're missing something like this:
interface IMyComponentProps {
someDefaultValue: string
}
interface IMyComponentState {
someValue: string
}
class App extends React.Component<IMyComponentProps, IMyComponentState> {
// ...
}
could you please try this as follow.
class App extends React.component<{},any>{ //your code}
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