Company logo
  • Empleos
  • Bootcamp
  • Acerca de nosotros
  • Para profesionales
    • Inicio
    • Empleos
    • Cursos y retos
    • Preguntas
    • Profesores
    • Bootcamp
  • Para empresas
    • Inicio
    • Nuestro proceso
    • Planes
    • Pruebas
    • Nómina
    • Blog
    • Comercial
    • Calculadora

0

166
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);
    }
7 months 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.

7 months ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos