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

fix(react): track owners separately, mutate updaters with dispatcher #130

Merged
26 changes: 17 additions & 9 deletions packages/react/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,17 @@ Object.defineProperties(Signal.prototype, {
});

// Track the current owner (roughly equiv to current vnode)
// let currentOwner: ReactOwner;
// Object.defineProperty(internals.ReactCurrentOwner, "current", {
// get() { return currentOwner; },
// set(owner) { currentOwner = owner; },
// });
let lastOwner: ReactOwner | undefined;
let currentOwner: ReactOwner | null = null;
Object.defineProperty(internals.ReactCurrentOwner, "current", {
get() {
return currentOwner;
},
set(owner) {
currentOwner = owner;
if (currentOwner) lastOwner = currentOwner;
},
});

// Track the current dispatcher (roughly equiv to current component impl)
let lock = false;
Expand All @@ -108,17 +114,19 @@ Object.defineProperty(internals.ReactCurrentDispatcher, "current", {
set(api) {
currentDispatcher = api;
if (lock) return;
if (api && !isInvalidHookAccessor(api)) {
if (lastOwner && api && !isInvalidHookAccessor(api)) {
// prevent re-injecting useReducer when the Dispatcher
// context changes to run the reducer callback:
lock = true;
const rerender = api.useReducer(UPDATE, {})[1];
lock = false;
const currentOwner = internals.ReactCurrentOwner.current;
let updater = updaterForComponent.get(currentOwner);

let updater = updaterForComponent.get(lastOwner);
if (!updater) {
updater = createUpdater(rerender);
updaterForComponent.set(currentOwner, updater);
updaterForComponent.set(lastOwner, updater);
} else {
updater._updater = rerender;
}
setCurrentUpdater(updater);
} else {
Expand Down
23 changes: 22 additions & 1 deletion packages/react/test/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
globalThis.IS_REACT_ACT_ENVIRONMENT = true;

import { signal, useComputed } from "@preact/signals-react";
import { createElement, useMemo } from "react";
import { createElement, useMemo, memo, StrictMode } from "react";
import { createRoot, Root } from "react-dom/client";
import { act } from "react-dom/test-utils";

Expand Down Expand Up @@ -158,5 +158,26 @@ describe("@preact/signals-react", () => {
});
expect(scratch.textContent).to.equal("bar");
});

it("should consistently rerender in strict mode", async () => {
const sig = signal<string>(null!);

const Test = memo(() => <p>{sig.value}</p>);
const App = () => (
<StrictMode>
<Test />
</StrictMode>
);

for (let i = 0; i < 3; i++) {
const value = `${i}`;

act(() => {
sig.value = value;
render(<App />);
});
expect(scratch.textContent).to.equal(value);
}
});
});
});