I am trying to use the useContext
from preact
. It seems I am doing something wrong, because my Context give undefined values.
Here is the main file:
const {render, h, createContext} = window.preact
import htm from 'https://unpkg.com/htm?module'
const html = htm.bind(h)
import SampleComp from './sample-comp.js'
export const ContextOne = createContext()
export const ContextTwo = createContext()
const RootComp = (props) => {
return html`
<ContextOne.Provider value=${'ContextOne'}>
<ContextTwo.Provider value=${'ContextTwo'}>
<${SampleComp}/>
</ContextTwo.Provider>
</ContextOne.Provider>
`
}
render(html`<${RootComp} />`, document.body);
and here is the sample component:
const {useContext} = window.preactHooks
const {h} = window.preact
import htm from 'https://unpkg.com/htm?module'
const html = htm.bind(h)
import {ContextOne, ContextTwo} from './index.js'
export default function SampleComp (props) {
const one = useContext(ContextOne)
const two = useContext(ContextTwo)
return html`<div>${one} - ${two}</div>`
}
What am I doing wrong here? I have been trying to figure it out for a few hours now but no idea.
question from:https://stackoverflow.com/questions/65540875/what-am-i-doing-wrong-with-usecontext