How to solve the error Uncaught TypeError: Cannot read properties of undefined in App.js in React
In the process of React development, we often encounter some errors. Here we look at an error reported in App.js. The error is as follows: App.js:69 Uncaught TypeError: Cannot read properties of undefined (reading 'setState') at onInputChange (App.js:69:1)
Below is the complete code that produces the error
// 调用 clarifai api接口
const app = new Clarifai.App({
apiKey : 'a2013154seuiasdfdse94302c5bd'
});
class App extends React.Component {
constructor () {
super();
this.state = {
input : '',
imageUrl : '',
}
}
onInputChange(event) {
this.setState({input: event.target.value})
console.log('setState input' + this.state.input);
}
onButtonSubmit() {
this.setState({imageUrl: this.state.input});
app.models.predict(Clarifai.COLOR_MODEL, this.state.input).then(
function(response) {
console.log(response);
},
function(err) {
// 这里处理错误
}
)
}
render(){
return (
<div className="App">
<Particles className="particles" id="tsparticles" options={particlesOptions} />
<Navigation />
<Logo />
<Rank />
<ImageLinkForm onInputChange={this.onInputChange} onButtonSubmit = {this.onButtonSubmit} />
<FaceRecognition imageUrl = {this.state.imageUrl} />
</div>
);
}
}
export default App;
There are few solutions to this problem.
As for why this happens, it is because the arrow method is not used in the above code. Here we have two solutions
-
We need to bind the handler.
constructor() { ...code this.onInputChange = this.onInputChange.bind(this) this.onButtonSubmit = this.onInputChange.bind(this) }
-
Change the method to arrow type (ES6 syntax).
onInputChange = (event) => { this.setState({input: event.target.value}) console.log('setState input' + this.state.input); } onButtonSubmit = () => { this.setState({imageUrl: this.state.input}); app.models.predict(Clarifai.COLOR_MODEL, this.state.input).then( function(response) { console.log(response); }, function(err) { // 这里处理错误 } ) }
We can choose any of the above two methods. Excluding the respective environmental factors, the above error can be solved.
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
React Tutorial - Transferring Props
Publish Date:2025/03/16 Views:185 Category:React
-
React transfers Props. Props are generated when components are encapsulated. Components expose some properties (Props) to the outside world to complete some functions.
React Tutorial: Props Anti-Pattern
Publish Date:2025/03/16 Views:183 Category:React
-
React's Props anti-pattern, using Props to generate state in getInitialState is an anti-pattern - Anti-Pattern.
React Tutorial - Props Validation
Publish Date:2025/03/16 Views:99 Category:React
-
Props validation is a very useful way to use components correctly. It can avoid many bugs and problems as your application becomes more and more complex. In addition, it can make your program more readable.
React tutorial: Types of Props for child components
Publish Date:2025/03/16 Views:170 Category:React
-
Usually, the child components of a React component are a group, that is, the child components are an array. Introduction to Type of the Children Props.
Rendered fewer hooks than expected error in React
Publish Date:2025/03/16 Views:50 Category:React
-
When we use a hook after a condition that may return a value, we get the error "Rendered fewer hooks than expected. This may be caused by an accidental early return statement". To fix this error, you need to move all React hooks to any condition that
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:117 Category:React
-
To resolve the “Uncaught ReferenceError: process is not defined” error in React, open a terminal in the root directory of your project and update the version of the `react-scripts` package by running `npm install react-scripts@latest` and reinstal
How to solve the "Function components cannot have string refs" error in React
Publish Date:2025/03/15 Views:139 Category:React
-
When we use a string as a reference in a function component, we get the error "Function components cannot have string refs". To fix this error, we need to use the useRef() hook to get a mutable ref object that we can use as a component.
React Error Property 'X' does not exist on type 'Readonly<{}>'
Publish Date:2025/03/15 Views:140 Category:React
-
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 of th