Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

418
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda