Skip to content

Commit

Permalink
Allow for context to propagate to components using context (#381)
Browse files Browse the repository at this point in the history
* Allow for context to propagate to components using context

* Create tasty-cooks-own.md

* add test

* Revert "add test"

This reverts commit fa03e5b.

* add test

* reproduce accurately
  • Loading branch information
JoviDeCroock committed Jul 6, 2023
1 parent 60dae3f commit e655e7f
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/tasty-cooks-own.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@preact/signals": patch
---

Allow for context to propagate to components using context
2 changes: 1 addition & 1 deletion packages/preact/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ hook(OptionsTypes.UNMOUNT, (old, vnode: VNode) => {

/** Mark components that use hook state so we can skip sCU optimization. */
hook(OptionsTypes.HOOK, (old, component, index, type) => {
if (type < 3)
if (type < 3 || type === 9)
(component as AugmentedComponent)._updateFlags |= HAS_HOOK_STATE;
old(component, index, type);
});
Expand Down
38 changes: 36 additions & 2 deletions packages/preact/test/index.test.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import {
signal,
computed,
useComputed,
useSignalEffect,
Signal,
signal,
} from "@preact/signals";
import { createElement, createRef, render } from "preact";
import { createElement, createRef, render, createContext } from "preact";
import { useContext, useState } from "preact/hooks";
import { setupRerender, act } from "preact/test-utils";

const sleep = (ms?: number) => new Promise(r => setTimeout(r, ms));
Expand Down Expand Up @@ -417,6 +418,39 @@ describe("@preact/signals", () => {
});
});

describe('hooks mixed with signals', () => {
it('signals should not stop context from propagating', () => {
const ctx = createContext({ test: 'should-not-exist' });
let update: any;

function Provider(props: any) {
const [test, setTest] = useState('foo');
update = setTest
return (
<ctx.Provider value={{ test }}>{props.children}</ctx.Provider>
);
}

const s = signal('baz')
function Test() {
const value = useContext(ctx);
return <p>{value.test} {s.value}</p>
}

function App() {
return <Provider><Test /></Provider>
}

render(<App />, scratch);

expect(scratch.innerHTML).to.equal('<p>foo baz</p>')
act(() => {
update('bar')
})
expect(scratch.innerHTML).to.equal('<p>bar baz</p>')
})
})

describe("useSignalEffect()", () => {
it("should be invoked after commit", async () => {
const ref = createRef();
Expand Down

0 comments on commit e655e7f

Please sign in to comment.