Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

274
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda