Repository navigation
|
for example: import { Accessor, Component, createContext, createEffect, createSignal, JSX, useContext, useTransition } from 'solid-js';
import { Dynamic } from 'solid-js/web';
const CounterContext = createContext<{ count: Accessor<number>, setCount: (count: number) => void}>()
const One = () => {
return <button onclick={() => {
const count = useContext(CounterContext)
console.log(count?.count());
}}>Click One</button>
}
const Two = () => {
return <button onclick={() => {
const count = useContext(CounterContext)
console.log(count?.count());
}}>Click Two</button>
}
const getContent = () => {
return () => {
const [, start] = useTransition()
const [comp, setComp] = createSignal<() => JSX.Element | undefined>()
const count = useContext(CounterContext)
createEffect(() => {
// if (count!.count() % 2 === 0) {
// setComp(() => One);
// } else {
// setComp(() => Two);
// }
start(() => {
if (count!.count() % 2 === 0) {
setComp(() => One);
} else {
setComp(() => Two);
}
})
})
return <Dynamic component={comp()}></Dynamic>
}
}
const Content = () => {
const C = getContent()
return <div>
<C />
</div>
}
const App: Component = () => {
const [count, setCount] = createSignal(0)
return (
<CounterContext.Provider value={{ count, setCount}}>
<Content />
<button onclick={() => setCount(count() + 1)}>add</button>
</CounterContext.Provider>
);
};
export default App;In start(() => {
if (count!.count() % 2 === 0) {
setComp(() => One);
} else {
setComp(() => Two);
}
})
if (count!.count() % 2 === 0) {
setComp(() => One);
} else {
setComp(() => Two);
}
|
Replies: 1 comment 1 reply
|
Why it seems to worked in the original example is because Bug with left up listener https://playground.solidjs.com/?hash=167881774&version=1.3.13 |
useContextmust be called inside an owner , to simply put, once the component initializes. Not inside an event handler where there's no owner usually but can be made to.Why it seems to worked in the original example is because
startTransitionhas a bug here where it left the owner and listener without cleaning them up after it's finished https://github.com/solidjs/solid/blob/main/packages/solid/src/reactive/signal.ts#L940-L941Bug with left up listener https://playground.solidjs.com/?hash=167881774&version=1.3.13