Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

174
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!