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

240
Vistas
How can I pass a state from a function component so i can use it in the use-Context

so am trying to get some data in a form of a state in react.js and i need to use it in the useContext so i can perform an Api call so please if someone can help that would be a please <3 and really helpful and thank you

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

0

If I understand your question correctly, you can do:

// MyComponent.js

import React, { useState, createContext } from "react";

// Note that you have to export the context to use it elsewhere
export const MyContext = createContext(null);

const MyComponent = () => {
 const [myState, setMyState] = useState();

 // Provider provides the context to components that are nested under it
 return (
  <MyContext.Provider value={{ myState: myState, setMyState: setMyState }}>
   <MyChildrenComponents />
  </MyContext.Provider>
 );
};

value is accessible in any component that's nested under the provider. In this case, the provider is <MyContext.Provider>. And value is accessible in <MyChildrenComponents />.

--

To extract value, we use useContext as such:

// MyChildrenComponents.js

import React, { useContext } from 'react';
import { MyContext } from './MyComponent.js';

const MyChildrenComponents = () => {
  const { myState, setMyState } = useContext(MyContext);
  
  // ...
}

You can read more about React context here (the docs).

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