0

I'd like to use the useContext and createContext with an arrow function. I want to be able to use variables inside Foo and pass them in createContext.

export const Foo = () => {
...
}

Combine above and below.

export const Bar = React.createContext({ lang: 'en'})
dannym
  • 95
  • 2
  • 10

1 Answers1

1

Given React documentation, it doesn't seem there is a way we can do this easily, one trick is to use a function self called :

React.createContext((() => {
   // any code here returning an object
   return {}
   // the function is self-called here
})())

but I would not recommend this way as it is not easily understandable and not so clean. However, it's the only way i can think about.

midugh
  • 608
  • 5
  • 21