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

417
Views
Why can't a React setState function be passed as a parameter to a class?

I have this class in my React project:

utils.ts

export class BtnManager {
  private setState: Function

  constructor(setState: React.Dispatch<React.SetStateAction<IBtnProps>>) {
    this.setState = setState;

  }

  public updateBtn(text?: string, disabled?: boolean, rest?: IBtnProps) {
    if (!rest) rest = INITIAL_BUTTON_STATE;
    if (text && disabled) {
      this.setState({
        ...rest,
        text,
        disabled,
      });
    } else {
      this.setState(rest);
    }
  }
}

And this useEffect in a component

Stake.tsx

useEffect(() => {
  const btnManager = new BtnManager(setBtnProps);
  const updateBtn = btnManager.updateBtn
    if (!accountId) {
      updateBtn();
    } else if (!tokenOut.info) {
      updateBtn('Select a Pool', true);
    }

 }, [accountId, tokenOut.info]
}

But am getting this error in the browser console:

Uncaught TypeError: Cannot read properties of undefined (reading 'setState')

I pass setState functions to components all the time, but this is the first time I am trying to create a class for helping manage state.I can console log this.setState inside the constructor after I initialize it and it logs the function signature, but when its used in the updateBtn method through the useEffect the above error is thrown. Not sure what is going on here, any insight appreciated.

about 4 years ago · Juan Pablo Isaza
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!