• Empleos
  • Sobre nosotros
  • profesionales
    • Inicio
    • Empleos
    • Cursos y retos
  • empresas
    • Inicio
    • Publicar vacante
    • Nuestro proceso
    • Precios
    • Evaluaciones
    • Nómina
    • Blog
    • Comercial
    • Calculadora de salario

0

419
Vistas
How to fix React Hook "useAppContext" cannot be called in a class component Error?

I want to use useContext() inside componentDidMount() function because I need Id Partition inside it but it gave me error :
React Hook "useAppContext" cannot be called in a class component. React Hooks must be called in a React function component or a custom React Hook function ?

 class Invhom extends Component {
   
            //...
    
     componentDidMount(){
                
             const {IdPartition,setIdPartition } = useAppContext();  // ... Error
            
                    let value = this.IdPartition;
                  //  console.log(value);
           axios.get('http://localhost:9091/inventaire/select_inventaire');
                  } }
            //...         
            }

//------------------------------context.js----------------------------------------


    export const AppContext = createContext(null);



    export function useAppContext() {
      return useContext(AppContext);
    }
about 3 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Please see https://reactjs.org/docs/hooks-rules.html :

Only Call Hooks from React Functions

You can't call Hooks from class components.

As noted in the FAQ, you do not have to rewrite your class components. However, please consider using function components and Hooks for new code.

about 3 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 Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recomiéndame algunas ofertas
Necesito ayuda