Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

150
Views
Using setState in 'async function with parameters' - used for onClick

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/regenerator-runtime@0.13.2/runtime.js"></script>
<script src="https://unpkg.com/@babel/standalone@7.10.3/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.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!