JIYIK CN >

Current Location:Home > Learning > WEB FRONT-END > React >

How to solve the error Uncaught TypeError: Cannot read properties of undefined in App.js in React

Author:JIYIK Last Updated:2025/03/15 Views:

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

  1. We need to bind the handler.
    constructor() {
    ...code
    this.onInputChange = this.onInputChange.bind(this)
    this.onButtonSubmit = this.onInputChange.bind(this)
    }
    
  2. 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.

Article URL:

Related Articles

How to avoid cross-origin (CORS) issues in React/Next.js

Publish Date:2025/03/17 Views:170 Category:NETWORK

In this article, we will introduce how to avoid cross-origin (CORS) issues in React/Next.js. Cross-origin resource sharing (CORS) is a protocol that defines how web requests should be handled when crossing different URLs.

React Tutorial - Transferring Props

Publish Date:2025/03/16 Views:188 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:187 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:102 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.

Why do you need to bind event handlers in React Class Components?

Publish Date:2025/03/16 Views:60 Category:React

When using React, we must have come across control components and event handlers. We need to use `.bind()` in the constructor of the custom component to bind these methods to the component instance. As shown in the following code:

Solution to the error "does not contain a default export" in React

Publish Date:2025/03/16 Views:191 Category:React

When we try to use `default import` to import from a module that does not have a `default export`, we get a "does not contain a default export" error. To fix the error, make sure the module has named exports and wrap the import in curly braces, e.g.

Solve the Module not found: Can't resolve 'react-bootstrap' error

Publish Date:2025/03/16 Views:90 Category:React

To resolve the error "Module not found: Error: Can't resolve 'react-bootstrap'", make sure to install the react-bootstrap package by opening a terminal in the root directory of the project and running the command `npm install react-bootstrap bootstrap

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial