React Error Property 'X' does not exist on type 'Readonly<{}>'
The React.js error “Property does not exist on type 'Readonly<{}>'” occurs when we try to access the props or state of an untyped class component. To fix the error, you need to use generics on the React.Component class to type the props or state object of that class.
Below is an example of where the error occurs.
import React from 'react';
class App extends React.Component {
constructor(props: any) {
super(props);
this.state = {value: ''}; // Using state, but we didn't enter it
}
handleChange = (event: any) => {
this.setState({value: event.target.value});
};
render() {
return (
<div>
<form>
{/*
⛔️ Error:
Property 'value' does not exist on type 'Readonly<{}>'.ts(2339)
*/}
<input
onChange={this.handleChange}
type="text"
value={this.state.value}
/>
<button type="submit">Submit</button>
</form>
</div>
);
}
}
export default App;
Notice that our class component has a value property in its state object.
The reason for the error is – we have not entered the state object of the class, so if we try to access any property of the state object, we will get an error.
The same is true for the props object - if we don't explicitly type it, trying to access a property on the props object will result in an error.
To resolve this error, you need to use React.Component
generics for the class React.Component<PropsObject, StateObject>
.
import React from 'react';
// We set props to an empty object and state to {value: string}
class App extends React.Component<{}, {value: string}> {
constructor(props: any) {
super(props);
this.state = {value: ''};
}
handleChange = (event: any) => {
this.setState({value: event.target.value});
};
render() {
return (
<div>
<form>
<input
onChange={this.handleChange}
type="text"
// ✅ Everything is working fine now
value={this.state.value}
/>
<button type="submit">Submit</button>
</form>
</div>
);
}
}
export default App;
We typed the value property on the state object in our class, so we can now access it as this.state.value .
We pass an empty object for the type of the props object since this class doesn't accept any props.
If we don't know how the props or state objects will be typed and want to disable type checking, use the any type.
import React from 'react';
// 👇️ Disable type checking for props and state
class App extends React.Component<any, any> {
constructor(props: any) {
super(props);
this.state = {value: ''};
}
handleChange = (event: any) => {
this.setState({value: event.target.value});
};
render() {
return (
<div>
<form>
<input
onChange={this.handleChange}
type="text"
value={this.state.value}
/>
<button type="submit">Submit</button>
</form>
</div>
);
}
}
export default App;
We used the any type when typing the props and state objects, which effectively turned off type checking.
Now we can access any property on the this.props and this.state objects without getting type checking errors.
Here is an example of a class that also explicitly types the props object.
import React from 'react';
// 👇️ type props as {name: string}, and state as {value: string}
class App extends React.Component<{name: string}, {value: string}> {
constructor(props: any) {
super(props);
this.state = {value: ''};
}
handleChange = (event: any) => {
this.setState({value: event.target.value});
};
render() {
return (
<div>
<form>
<input
onChange={this.handleChange}
type="text"
value={this.state.value}
/>
<button type="submit">Submit</button>
</form>
<h1>{this.props.name}</h1>
</div>
);
}
}
export default App;
We explicitly typed the props object of the App component to have a name property of type string. Now, when we use the component, we must provide a name property, for example <App name="James Doe" />
.
To resolve the React.js error "Property does not exist on type 'Readonly<{}>'", make sure to explicitly type the class's props or state object, adding any properties we intend to access on the props or state object
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Fix Uncaught ReferenceError: useState is not defined in React
Publish Date:2025/03/15 Views:142 Category:React
-
When we use the useState hook in our code but forget to import it, it generates the error Uncaught ReferenceError: useState is not defined. To fix this error, you need to import the hook before using it import {useState} from react . // ?️
React error Uncaught ReferenceError: process is not defined solution
Publish Date:2025/03/15 Views:116 Category:React
-
要解决 React 中的“Uncaught ReferenceError: process is not defined” 错误,需要在项目的根目录中打开终端并通过运行 `npm install react-scripts@latest` 更新 `react-scripts` 包的版本,并在必要时重新安装
How to solve the "Function components cannot have string refs" error in React
Publish Date:2025/03/15 Views:138 Category:React
-
当我们在函数组件中使用字符串作为引用时,会出现错误“Function components cannot have string refs”。 要解决该错误,需要使用 useRef() 钩子来获取一个可变的 ref 对象,我们可以将其用作组件
Solve React's Unexpected default export of anonymous function error
Publish Date:2025/03/15 Views:119 Category:React
-
When we try to export an anonymous function using default export, it results in the warning “Unexpected default export of anonymous function.” To fix this error, you need to give the function a name before exporting it.
React error Type '() => JSX.Element[]' is not assignable to type FunctionCompo
Publish Date:2025/03/15 Views:51 Category:React
-
When we try to return an array of elements from a function component, a React.js error “Type '() => JSX.Element[]' is not assignable to type FunctionComponent” occurs. To fix the error, you need to wrap the array of elements in a React fragment.
How to solve React Type {children: Element} has no properties in common with type
Publish Date:2025/03/15 Views:103 Category:React
-
React.js error "Type {children: Element} has no properties in common with type IntrinsicAttributes" occurs when we try to pass children prop to a component without any props. To resolve the error, you need to define and type the properties on the comp
Fix the value prop on input should not be null error in React
Publish Date:2025/03/15 Views:141 Category:React
-
The warning "value prop on input should not be null" is caused when we set the initial value of an input to null or override the initial value setting it to null, e.g. from an empty API response. Use a fallback value to solve this problem.
Property does not exist on type 'JSX.IntrinsicElements' error in React
Publish Date:2025/03/15 Views:188 Category:React
-
When the name of a component starts with a lowercase letter, an error "Property does not exist on type 'JSX.IntrinsicElements" will appear. To fix this error, you need to make sure you always start your component names with an uppercase letter, instal
React uses Router to get the current route
Publish Date:2025/03/15 Views:99 Category:React
-
Use the `useLocation()` hook to get the current route through React Router, such as `const location = useLocation()`. The hook returns the current location object. For example, we can access the pathname as location.pathname.