Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Property "value" does not exist on type Readonly

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?

like image 960
MVG Avatar asked Feb 19 '18 15:02

MVG


People also ask

Does not exist in type readonly?

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.

Why does react have state and props both how are they different?

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.

How do you make a state in react?

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).

How do you define a state in react typescript?

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.

How to fix “property ‘value’ does not exist on type ‘readonly< {}>’”?

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> { //...

How to fix property 'value' does not exist on type 'htmlelement'?

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!

Which react modal property does not exist on readonly type?

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.

Why is my data not persist in local component state?

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!


Video Answer


2 Answers

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> {
  // ...
}
like image 51
Anthony N Avatar answered Sep 21 '22 23:09

Anthony N


could you please try this as follow.

class App extends React.component<{},any>{ //your code}
like image 45
Kelum Sampath Edirisinghe Avatar answered Sep 20 '22 23:09

Kelum Sampath Edirisinghe