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

175
Vistas
Add useContext() on JSON export

I want to create a list of functionalities that I can access easily from anywhere on my app by simply importing the component. Here is what my component looks like:

functionalities.component.jsx

import { AES } from "crypto-js"
import { useContext, useState } from "react"
import { ConfigurationContext } from "../env"

const {configurationState} = useContext(ConfigurationContext);

const Functionalities = {
  encrypt: (info) => AES.encrypt(info, configurationState.application.key).toString();
}

export default Functionalities

The problem I'm facing now is that I'm not able to use any context values since it would cause an error. Is there a way to implement "useContext" on this?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can call a React Hook only inside a React component or inside a custom hook, it's one of the rules of the hooks.

The best you could do, if you need to share common functionalities, is creating a set of custom hooks.

import { AES } from "crypto-js"
import { useContext } from "react"
import { ConfigurationContext } from "../env"


const Functionalities = {
  useEncrypt: () => {
    const { configurationState } = useContext(ConfigurationContext);
    return (info) => AES.encrypt(info, configurationState.application.key).toString();
  }
};

export default Functionalities;

Example usage (always remember to call useContext inside a Context.Provider).

function EncryptComponent({info}) {
  const encrypt = Functionalities.useEncrypt();
  return <button onClick={() => encrypt(info)}>Encrypt</button>
}

I provide a CodeSandbox example that show how to do that.

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