Skip to content

Commit 0fd9503

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 0fd9503

2 files changed

Lines changed: 9 additions & 5 deletions

File tree

.changeset/odd-tigers-remember.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@preact/signals": patch
3+
---
4+
5+
Fix prefresh HMR not working with `useSignal`.

packages/preact/src/index.ts

Lines changed: 4 additions & 5 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, useMemo, useEffect, useState } from "preact/hooks";
33
import {
44
signal,
55
computed,
@@ -387,10 +387,9 @@ 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>) {

0 commit comments

Comments
 (0)