Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow for context to propagate to components using context #381

Merged
merged 6 commits into from
Jul 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading