-1

I am running a next.js app, with a file "temp.js" in the /pages directory. My code (temp.js) is as follows:

import React from 'react';
import {Button} from '@mui/material';

class SomeClass extends React.Component{
    state={
        someState: false
    }
    handleClick = async(someValue) => {
        this.setState({ someState: true });
        // await someAsyncFunction(someValue);
        console.log(someValue);
    }
    render(){
        return(
            <Button onClick={()=>this.handleClick(12)}>
                Click me.!
            </Button>
        )
    }
}

export default SomeClass;

Stack Snippet (using button instead of MUI Button, but that doesn't matter):

<div id="root"></div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.2/umd/react.development.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.2/umd/react-dom.development.js"></script>

<script type="text/babel" data-presets="es2017,react,stage-3">
class SomeClass extends React.Component{
    state={
        someState: false
    }
    handleClick = async(someValue) => {
        this.setState({ someState: true });
        // await someAsyncFunction(someValue);
        console.log(someValue);
    }
    render(){
        return(
            <button onClick={()=>this.handleClick(12)}>
                Click me.!
            </button>
        )
    }
}

ReactDOM.render(<SomeClass />, document.getElementById("root"));
</script>
<script src="https://unpkg.com/[email protected]/runtime.js"></script>
<script src="https://unpkg.com/@babel/[email protected]/babel.min.js"></script>

This gives the following error:

Unhandled Runtime Error
TypeError: Cannot read properties of undefined (reading 'setState')

   7 | }
   8 | handleClick = async(someValue) => {
>  9 |     this.setState({ someState: true });
     |         ^
  10 |     console.log(someValue);
  11 | }
  12 | render(){

This function would work fine if the 'someValue' was not required. Need help. Please ask for any clarifications.

16
  • 4
    "This gives the following error:" What error? All you've shown is something pointing to a .. Other than not handling rejections (see below), that code looks like it should run. Commented Jan 31, 2022 at 9:34
  • 1
    Side note: If you're calling an async function from a non-async function, always be sure to handle promise rejections. Otherwise, they won't be handled by anything. Commented Jan 31, 2022 at 9:35
  • 2
    Thank you for updating the question with the error, but the code shown in the question will definitely not produce that error. handleClick is an arrow function in a context where it will close over this correctly, so this will not be undefined. The code you're running that has this error must be different from the above. Please update your question with a minimal reproducible example demonstrating the problem, ideally a runnable one using Stack Snippets (the [<>] toolbar button). Stack Snippets support React, including JSX; here's how to do one. Commented Jan 31, 2022 at 9:40
  • 1
    If calling handleClick throws an error then adding logic that deals with the return value of handleClick isn't going to help. Commented Jan 31, 2022 at 9:42
  • 2
    @KonarkVerma - I've copied your code into a Stack Snippet (using button instead of MUI's Button, which is not going to affect what you've described). As you can see, it works just fine. Please edit the code in the script type="text/babel" tag in the snippet with your real code causing the error. Commented Jan 31, 2022 at 9:45

1 Answer 1

1

Have you tried deleting your node-modules, and doing an npm i again? It works for me sometimes.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.