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

275
Visualizações
How to use a service class in a react hook?

I am unsuccessfully trying to initialise and use a service class from a React hook. The service class looks like:

export class ExampleService {
  constructor(private exampleParams: string[]) {}

  isPresent(exampleName: string): boolean {
    return this.exampleParams.includes(exampleName);
  }

  getAll(): string[] {
    return this.exampleParams;
  }

  setExamples(exampleParams: string[]): void {
    this.exampleParams = exampleParams;
  }
}

And then in the hook I am trying to use this class:

import { ExampleService } from '../somewhere/example-service';
import { useContext } from 'react';

import { ExampleContext } from '../contexts/example-context';

export const useExample = () => {
  const { currentExamples } = useContext(ExampleContext);
  const checkExamples = new ExampleService(currentExamples);

  return [checkExamples.isPresent];
};

However, I am getting TypeError: Cannot read property 'exampleParams' of undefined when I try to test the code :/ (The error is on isPresent function above).

What am I doing wrong?

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