Skip to content

Commit 9e92589

Browse files
fix: make prefresh hmr integration easier
The callback to `useMemo` is assumed to be pure during HMR and will be re-triggered. This in turn broke the `useSignal` hooks HMR as they used side-effectful callbacks.
1 parent 6cc7005 commit 9e92589

1 file changed

Lines changed: 7 additions & 8 deletions

File tree

packages/preact/src/index.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { options, Component, isValidElement, Fragment } from "preact";
2-
import { useRef, useMemo, useEffect } from "preact/hooks";
2+
import { useRef, useEffect, useState } from "preact/hooks";
33
import {
44
signal,
55
computed,
@@ -92,7 +92,7 @@ function SignalValue(this: AugmentedComponent, { data }: { data: Signal }) {
9292
const currentSignal = useSignal(data);
9393
currentSignal.value = data;
9494

95-
const [isText, s] = useMemo(() => {
95+
const [[isText, s]] = useState(() => {
9696
let self = this;
9797
// mark the parent component as having computeds so it gets optimized
9898
let v = this.__v;
@@ -139,7 +139,7 @@ function SignalValue(this: AugmentedComponent, { data }: { data: Signal }) {
139139
};
140140

141141
return [isText, wrappedSignal];
142-
}, []);
142+
});
143143

144144
// Rerender the component whenever `data.value` changes from a VNode
145145
// to another VNode, from text to a VNode, or from a VNode to text.
@@ -387,17 +387,16 @@ Component.prototype.shouldComponentUpdate = function (
387387
export function useSignal<T>(value: T, options?: SignalOptions<T>): Signal<T>;
388388
export function useSignal<T = undefined>(): Signal<T | undefined>;
389389
export function useSignal<T>(value?: T, options?: SignalOptions<T>) {
390-
return useMemo(
391-
() => signal<T | undefined>(value, options as SignalOptions),
392-
[]
393-
);
390+
return useState(() =>
391+
signal<T | undefined>(value, options as SignalOptions)
392+
)[0];
394393
}
395394

396395
export function useComputed<T>(compute: () => T, options?: SignalOptions<T>) {
397396
const $compute = useRef(compute);
398397
$compute.current = compute;
399398
(currentComponent as AugmentedComponent)._updateFlags |= HAS_COMPUTEDS;
400-
return useMemo(() => computed<T>(() => $compute.current(), options), []);
399+
return useState(() => computed<T>(() => $compute.current(), options))[0];
401400
}
402401

403402
function safeRaf(callback: () => void) {

0 commit comments

Comments
 (0)