Control Flow primitives and components that require specifying explicit keys to identify or rerender elements.
- keyArray - Reactively maps an array by specified key with a callback function - underlying helper for the control flow. - Key - Creates a list of elements by mapping items by provided key. - Entries - Creates a list of elements by mapping object entries. - MapEntries - Creates a list of elements by mapping Map entries. - SetValues - Creates a list of elements by mapping Set values. - Rerun - Causes the children to rerender when the on changes.
Installation
``bash npm install @solid-primitives/keyed
or
yarn add @solid-primitives/keyed `
keyArray
Reactively maps an array by specified key with a callback function - underlying helper for the control flow.
$3
#### Import
`ts import { keyArray } from "@solid-primitives/keyed"; `
#### Basic usage
The keyArray primitive takes 4 arguments:
- list - input list of values to map - keyFn - key getter, items will be identified by it's value. changing the value is changing the item. - mapFn - reactive function used to create mapped output item. Similar to Array.prototype.map but both item and index are signals, that could change over time. - options - a fallback for when the input list is empty or missing _(Optional)_
return { id: model.id, get name() { return name(); }, get description() { return description(); }, get index() { return index(); }, setName, setDescription, }; }, ); `
Notice that both the value and index arguments are signals. Items are identified only by keys, it means that the items could be copied, replaced, changed, but as long as the key is the same, keyArray will treat it as the same item.
Creates a list of elements by mapping items by provided key. Similar to Solid's and , but here, both value and index arguments are signals.
But changing the value does not rerender the element, only where the value is being used.
$3
#### Import
`ts import { Key } from "@solid-primitives/keyed"; `
#### Typical usage
Both each and by have to be provided. The fallback prop is optional, it will be displayed when the list in each is missing or empty.
`tsx item.id} fallback={
No items
}> {item =>
{item()}
}
`
#### Key shortcut
prop by can also be an object key
`tsx `
#### Index argument
Second argument of the map function is an index signal.
Creates a list of elements by mapping object entries. Similar to Solid's and , but here, render function takes three arguments, and both value and index arguments are signals.
$3
`tsx import { Entries } from "@solid-primitives/keyed";
No items
}> {(key, value) => (
{key}: {value()}
)} ; `
$3
Third argument of the map function is an index signal.
`tsx No items
}> {(key, value, index) => (
{key}: {value()}
)}
`
Creates a list of elements by mapping Map entries. Similar to Solid's
and , but here, render function takes three arguments, and both value and index arguments are signals.
$3
`tsx import { MapEntries } from "@solid-primitives/keyed";
const [map, setMap] = createSignal(new Map());
No items
}> {(key, value) => (
{key}: {value()}
)} ; `
$3
Third argument of the map function is an index signal.
MapEntries is using Map#keys() so the index and resulting JSX will follow the insertion order.`tsx No items
}> {(key, value, index) => (
{key}: {value()}
)}
`
Creates a list of elements by mapping Set values. Similar to Solid's
and , but here, render function takes two arguments, the value and the index argument as a signal.
$3
`tsx import { SetValues } from "@solid-primitives/keyed";
const [set, setSet] = createSignal(new Set());
No items
}> {value =>
{value}
} ; `
$3
Second argument of the map function is an index signal.
SetValues is using Set#values() so the index and resulting JSX will follow the insertion order.`tsx No items
}> {(value, index) =>
{value}
}
`
Causes the children to rerender when the
on key changes. Equivalent of v-key in vue, and {#key} in svelte.
> Note: Since Solid 1.5.0 the
component has a keyed prop that works very similarly to .
$3
`ts import { Rerun } from "@solid-primitives/keyed"; `
$3
You have to provide a
on prop. Changing it, will cause the children to rerender.`tsx const [count, setCount] = createSignal(0);
// will rerender whole
// or pass a function count()}/>
// or an array of dependencies
`
#### Passing a function as children
You can treat
on prop like sources argument of the Solid's on helper, and the children as the second, callback argument.`tsx {([count, className]) => ( setCount(p => ++p)}> {count}
)}
`
#### Using with Transition
can be used together with solid-transition-group to animate single component's transition, on state change.`tsx setCount(p => ++p)}>{count()}