Solid Primitives Rootless

# @solid-primitives/rootless [![size](https://img.shields.io/bundlephobia/minzip/@solid-primitives/rootless?style=for-the-badge&label=size)](https://bundlephobia.com/package/@solid-primitives/rootless) [![version](https://img.shields.io/npm/v/@solid-primitives/rootless?style=for-the-badge)](https://www.npmjs.com/package/@solid-primitives/rootless) [![stage](https://img.shields.io/endpoint?style=for-the-badge&url=https%3A%2F%2Fraw.githubusercontent.com%2Fsolidjs-community%2Fsolid-primitives%2Fmain%2Fassets%2Fbadges%2Fstage-2.json)](https://github.com/solidjs-community/solid-primitives#contribution-process) A collection of helpers that aim to simplify using reactive primitives outside of reactive roots, and managing disposal of reactive roots. - [`createSubRoot`](#createsubroot) - Creates a reactive **sub root**, that will be automatically disposed when it's owner does. - [`createCallback`](#createcallback) - A wrapper for creating callbacks with `runWithOwner`. - [`createDisposable`](#createdisposable) - For disposing computations early – before the root cleanup. - [`createSingletonRoot`](#createsingletonroot) - Share "global primitives" across multiple reactive scopes. - [`createRootPool`](#createrootpool) - Creates a pool of reactive roots, that can be reused. ## Installation ```bash npm install @solid-primitives/rootless # or pnpm add @solid-primitives/rootless # or yarn add @solid-primitives/rootless ``` ## `createSubRoot` ###### Previously `createSubRoot` Creates a reactive **root branch**, that will be automatically disposed when it's owner does. ### How to use it Use it for nested roots _(literally nested, or provided manually to dependency array)_ that should be disposed before or with their owner. ```ts import { createSubRoot } from "@solid-primitives/rootless"; createRoot(dispose => { createSubRoot(disposeBranch => { // computations will be disposed with branch createEffect(() => {...}); // disposes only the branch disposeBranch(); }); // disposes the outer root, AND all the nested branches dispose(); }); ``` ### Definition ```ts function createSubRoot(fn: (dispose: VoidFunction) => T, ...owners: (Owner | null)[]): T; ``` ## `createCallback` A wrapper for creating callbacks with `runWithOwner`. It gives you the option to use reactive primitives after root setup and outside of effects. **Why?** All of the callback-based (in opposite to effect-based) events is Solid don't have a root, because the root is changed synchronously after initialization finishes. So normally that would prevent you from using reactive primitives there. `runWithOwner` attatches whatever computations created inside, to the owner passed to it. ### How to use it ```tsx // in component body const handleClick = createCallback(() => { // the effect will be created normally, attached to the component's reactive root createEffect(() => {...}) }) // in jsx ; }); return {useCounter(1)}; ``` ### Usage with `` `createRootPool` can be combined with `` (or any other control-flow component) to reuse already created HTML Elements while getting the same benefits of stable connection between rendered elements and the reference to the source item. ```tsx const pool = createRootPool(item => ) return {item => pool(item)} ``` > **Warning** > Using `createRootPool` with `` creates an un-keyed control-flow — meaning that a single element can be reused by more than one item (not at the same time). Which can cause styles, animations, and other side effects to leak between items. > It's meant to be used only as a performance optimization, and only when you're sure that the side effects won't leak between items. ## Changelog See [CHANGELOG.md](./CHANGELOG.md)