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

170
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
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