Skip to content

Sync between useSignal and its parameter #247

Open
@billybimbob

Description

@billybimbob

For both React and Preact, the useSignal hook implementation is currently defined as:

export function useSignal<T>(value: T) {
return useMemo(() => signal<T>(value), []);
}

This implementation would cause the returned signal's .value to only be kept in sync with the value parameter on the very first call.

let param = 0;

// inside a compoenent

const num = useSignal(param); // num.value is 0

// re-render component with a modified param

param = 5;

const num = useSignal(param); // num.value is still 0

One way to get around this behavior is to immediately set the .value on the returned signal:

const currentSignal = useSignal(data);
currentSignal.value = data;

I am wondering, is there a specific reason that the .value is not set directly in the useSignal function? One possible limitation I can see is that batching multiple signal updates would become not possible.

I do think however that the benefit of synchronizing between a signal's .value and its value parameter outweighs that limitation.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions