useContext not working properly

i am trying to use createContext in my solid app but it is not working properly giving undefined
11 Replies
Perspective
PerspectiveOP7d ago
hey i am getting undefined in the context consumer ... i am directly giving signals value to the provider
export interface optionsType {
source_id: string,
}
export interface ContextType{
Options : () => optionsType,
setOptions: Setter<optionsType>
}
export const MyContext = createContext<ContextType | undefined>()

....
const [Options, setOptions] = createSignal({
source_id: "xyz",
});
<MyContext.Provider value={{ setOptions, Options }}>
export interface optionsType {
source_id: string,
}
export interface ContextType{
Options : () => optionsType,
setOptions: Setter<optionsType>
}
export const MyContext = createContext<ContextType | undefined>()

....
const [Options, setOptions] = createSignal({
source_id: "xyz",
});
<MyContext.Provider value={{ setOptions, Options }}>
in the consumer
const value = useContext(MyContext)
console.log(value) // undefined
const value = useContext(MyContext)
console.log(value) // undefined
jer3m01
jer3m017d ago
try value.Options()
Perspective
PerspectiveOP7d ago
lol the value itself is undefined
jer3m01
jer3m017d ago
ah yeah my bad
Perspective
PerspectiveOP7d ago
ok some weird stuff in my vs code's console the values is showing up but no in browser when i am manually saving the app.tsx it starting showing my browser also but if i do anything beside app.tsx it again showing undefined
jer3m01
jer3m017d ago
if you log onMount does it show value?
Perspective
PerspectiveOP7d ago
lemme try nope can you come vc :(
jer3m01
jer3m017d ago
cant but can u send the entire component?
Perspective
PerspectiveOP7d ago
ahh it's not small i sent the smallest i could do i mean i can show you live would be better at solving also to add.. i need to wrap suspense inside provide to actually see the result if i am creating context in components it is not showing anything @jer3m01
jer3m01
jer3m017d ago
can't help much without the code try creating a new context and component without anything else, if that works copy over piece by piece the rest
Perspective
PerspectiveOP7d ago
ok rookie mistake ig. was creating context in the same file.. which ig hot reload the file whenever another component tries to access it

Did you find this page helpful?