Skip to content

useSignalEffect should execute after changes have been applied to the DOM #228

@luisherranz

Description

@luisherranz

The callback passed to useSignalEffect should be executed after the changes (if any) have been applied to the DOM. Otherwise, it won't be a direct replacement for useEffect.

For example, people expect this to work, even if ref appears when open switches from false to true, because useEffect waits until changes have been applied to the DOM:

useEffect(() => {
  if (open) ref.current.focus();
}, [open]);

But with the current implementation of useSignalEffect, the callback runs before the DOM changes, and therefore ref.current may not exist yet:

useSignalEffect(() => {
  if (open.value) ref.current.focus();
});

Full example in StackBlitz.

Other reactive libraries introduce a tick function (Vue, Svelte, Alpine), but I'd prefer if useSignalEffect could mimic useEffect because the familiarity and ergonomics are better. I.e., access to signals after the tick() call are not tracked.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions