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

173
Vistas
TypeScript in createContext issue

On my app I have this:

const restaurantsArr = [{ name: McDonald, rating: 5 }]
const RestaurantContext = createContext();
const [restaurants, setRestaurants] = useState(restaurantsArr);

For Provider, I want to pass my destructured variables from useState.

<Provider value={[restaurants, setRestaurants]} />

TS yelling at me, that createContext must have value, but if my createContext has empty array for example I have errors.

How I should create interface ContextProps?

In fact, I have array which contains another array of objects and a function.

I'm fighting with this for a couple of hours and I have warnings and errors all the time...

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

0

Ok, so I have this now:

export const RestaurantsContext = createContext<any>([]);

interface Props {
    children: ReactElement;
}

const RestaurantsProvider: React.FC<Props> = ({children}) => {
const [restaurants, setRestaurants] = useState(restaurantsArr)

   return (
      <RestaurantsContext.Provider value={[restaurants, setRestaurants]}>
         {children}
      </RestaurantsContext.Provider>
    )

But It's just walk-around with any on export const RestaurantsContext = createContext<any>([]).

What I want to do is to have something like:

interface ContextProps {
   //and something here
}
     

But how should I type [data, setData] ? It's an array with array of objects and with setState function.

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