SolidJS style wrapper on alien-signals.
npm install solid-alien-signalsA SolidJS-like API for alien-signals, providing familiar primitives such as batch, createEffect, createMemo, createResource, createSignal, and untrack.
> Note:
> Some methods in this package are direct aliases for primitives from alien-signals (such as createEffect and createMemo). Others, like batch, createResource, createSignal, and untrack, are wrappers or adapted implementations to provide a SolidJS-compatible API and behavior.
- SolidJS-compatible API: Use batch, createEffect, createMemo, createResource, createSignal, and untrack, similar to SolidJS.
- Resource primitives: Use createResource for async data flows.
- Type guards: Use isSignal and isEffect to identify reactive primitives.
- TypeScript support: Fully typed API.
``sh`
npm install solid-alien-signals
`ts
import { createSignal, createEffect, createMemo } from 'solid-alien-signals';
const [count, setCount] = createSignal(1);
const double = createMemo(() => count() * 2);
createEffect(() => {
console.log('Count is', count());
});
setCount(2); // Console: Count is 2
console.log(double()); // 4
// Setters also accept functions for updates based on previous value
setCount((prev) => prev + 1); // count is now 3
`
Batch multiple state updates into a single re-computation:
`ts
import { batch, createSignal, createEffect } from 'solid-alien-signals';
const [a, setA] = createSignal(1);
const [b, setB] = createSignal(2);
createEffect(() => {
console.log('Sum:', a() + b());
});
batch(() => {
setA(3);
setB(4);
});
// Console: Sum: 7 (only runs once)
`
Handle asynchronous data with createResource:
`ts
import { createResource } from 'solid-alien-signals';
const [user, { refetch }] = createResource(async () => fetch('/api/user').then((res) => res.json()));
console.log(user.loading); // true/false
console.log(user()); // user data or undefined
`
Prevent dependency tracking inside a function:
`ts
import { createSignal, untrack } from 'solid-alien-signals';
const [count, setCount] = createSignal(0);
const value = untrack(() => count());
// value is read without tracking as a dependency`
Check if a value is a signal or effect:
`ts
import { createSignal, createEffect, isSignal, isEffect } from 'solid-alien-signals';
const [count, setCount] = createSignal(0);
const effectFn = createEffect(() => console.log(count()));
console.log(isSignal(count)); // true
console.log(isSignal(setCount)); // true
console.log(isEffect(effectFn)); // true
console.log(isSignal(() => {})); // false
`
Batches multiple updates into a single re-computation.
Runs a function whenever its dependencies change.
Creates a memoized computation.
Runs a function whenever its dependencies change.
Manages async data with loading/error states.
Creates a reactive signal. The setter accepts either a value or a function that receives the previous value.
Runs a function without tracking dependencies.
Returns true if the value is a signal (including signal getters, setters, memos, and resources).
Returns true` if the value is an effect function.
Portions of this code are adapted from alien-signals and SolidJS both MIT licensed.