Skip to content

Latest commit

 

History

History
32 lines (24 loc) · 1.08 KB

File metadata and controls

32 lines (24 loc) · 1.08 KB
title createSelector
function createSelector<T, U>(
	source: () => T,
	fn?: (a: U, b: T) => boolean
): (k: U) => boolean

Creates a conditional signal that only notifies subscribers when entering or exiting their key matching the value. Useful for delegated selection state. As it makes the operation O(1) instead of O(n).

const isSelected = createSelector(selectedId)

<For each={list()}>
	{(item) => <li classList={{ active: isSelected(item.id) }}>{item.name}</li>}
</For>

In the above code if the item.id is equal to the selectedId the active class will be added to the li element. If the item.id is not equal to the selectedId the active class will be removed from the li element.

Arguments

Name Type Description
source () => T The source signal to get the value from.
fn (a: U, b: T) => boolean A function to compare the key and the value.