Skip to content

Commit

Permalink
Merge pull request #59 from luisherranz/support-preact-signals-react-2
Browse files Browse the repository at this point in the history
Support @preact/signals-react 2
  • Loading branch information
luisherranz committed Jan 11, 2024
2 parents 50c25a8 + 0c740e7 commit 7cf94a9
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 24 deletions.
5 changes: 5 additions & 0 deletions .changeset/strong-adults-march.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"deepsignal": minor
---

Add support for @preact/signals-react 2.0.0
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ import { deepSignal } from "deepsignal/react";
const state = deepSignal({});
```

- 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.
- For `useDeepSignal`, no integration is required.

### Without Preact/React

```sh
Expand Down
8 changes: 4 additions & 4 deletions packages/deepsignal/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@
"prepublishOnly": "cp ../../README.md . && cd ../.. && pnpm build"
},
"peerDependencies": {
"@preact/signals-core": "^1.3.1",
"@preact/signals-core": "^1.5.1",
"@preact/signals": "^1.1.4",
"@preact/signals-react": "^1.3.3",
"@preact/signals-react": "^1.3.8 || ^2.0.0",
"preact": "^10.16.0"
},
"peerDependenciesMeta": {
Expand All @@ -67,9 +67,9 @@
"devDependencies": {
"preact": "10.9.0",
"preact-render-to-string": "^5.2.4",
"@preact/signals-core": "^1.3.1",
"@preact/signals-core": "^1.5.1",
"@preact/signals": "^1.1.4",
"@preact/signals-react": "^1.3.3",
"@preact/signals-react": "^2.0.0",
"@types/react": "^18.0.18",
"@types/react-dom": "^18.0.6",
"react": "^18.2.0",
Expand Down
4 changes: 2 additions & 2 deletions packages/deepsignal/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
"source": "src/index.ts",
"license": "MIT",
"dependencies": {
"@preact/signals-core": "^1.3.1",
"@preact/signals-react": "^1.3.3"
"@preact/signals-core": "^1.5.1",
"@preact/signals-react": "^2.0.0"
},
"peerDependencies": {
"react": "17.x || 18.x"
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
21 changes: 13 additions & 8 deletions packages/deepsignal/react/test/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,24 @@ import { useDeepSignal, type DeepSignal } from "deepsignal/react";
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 @@ -37,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
20 changes: 10 additions & 10 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 7cf94a9

Please sign in to comment.