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

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

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 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