Skip to content

Commit

Permalink
Force useDeepSignal to track the component
Browse files Browse the repository at this point in the history
  • Loading branch information
luisherranz committed Jan 11, 2024
1 parent 9b0ebbb commit 21b7bf2
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .changeset/strong-adults-march.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
"deepsignal": minor
---

Add a peer dependency for @preact/signals-react 2.0.0
Add support for @preact/signals-react 2.0.0
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ import { deepSignal } from "deepsignal/react";
const state = deepSignal({});
```

Starting from `@preact/[email protected]`, you should also follow the [React integration guide of `@preact/signals-react`](https://github.com/preactjs/signals/blob/main/packages/react/README.md#react-integration) to choose one of the integration methods.
- If you want to use `deepSignal` outside of the components, please follow the [React integration guide of `@preact/signals-react`](https://github.com/preactjs/signals/blob/main/packages/react/README.md#react-integration) to choose one of the integration methods.
- If rely exclusively on `useDeepSignal`, no integration is required.

### Without Preact/React

Expand Down
2 changes: 2 additions & 0 deletions packages/deepsignal/react/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import "@preact/signals-react";
import { useMemo } from "react";
import { deepSignal, type DeepSignal } from "../../core/src";
import { useSignals } from "@preact/signals-react/runtime";

export const useDeepSignal = <T extends object>(obj: T): DeepSignal<T> => {
useSignals();
return useMemo(() => deepSignal(obj), []);
};

Expand Down
22 changes: 13 additions & 9 deletions packages/deepsignal/react/test/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,28 @@ import { createElement } from "react";
import { createRoot, Root } from "react-dom/client";
import { act } from "react-dom/test-utils";
import { useDeepSignal, type DeepSignal } from "deepsignal/react";
import "@preact/signals/auto";

describe("deepsignal/react", () => {
let scratch: HTMLDivElement;
let root: Root;
function render(element: Parameters<Root["render"]>[0]) {
act(() => root.render(element));
}
let render: Root["render"];

const window = globalThis as any;
beforeEach(async () => {
scratch = document.createElement("div");
document.body.appendChild(scratch);

beforeEach(() => {
scratch = window.document.createElement("div");
root = createRoot(scratch);
const realRoot = createRoot(scratch);
root = {
render: element => act(() => realRoot.render(element)),
unmount: () => act(() => realRoot.unmount()),
};

render = root.render.bind(root);
});

afterEach(() => {
act(() => root.unmount());
scratch.remove();
});

describe("useDeepSignal", () => {
Expand All @@ -38,7 +42,7 @@ describe("deepsignal/react", () => {
}

// @ts-ignore
render(<App />);
await render(<App />);

expect(scratch.textContent).to.equal("test");
expect(spy).to.be.calledOnce;
Expand Down

0 comments on commit 21b7bf2

Please sign in to comment.