{
  "id": "c226a46911b30c930f2bdb96",
  "source": "solid:signals",
  "type": "github-document",
  "title": "create-root",
  "content": "---\ntitle: createRoot\nuse_cases: \u003e-\n  memory management, nested tracking scopes, non-auto-disposing scopes, manual\n  cleanup control, top-level code wrapping\ntags:\n  - roots\n  - memory\n  - scopes\n  - disposal\n  - tracking\nversion: \"1.0\"\ndescription: \u003e-\n  Create non-tracked owner scopes in SolidJS for manual memory management.\n  Essential for nested tracking scopes and preventing auto-disposal.\n---\n\nThe `createRoot` function creates a new owned context, which requires explicit disposal of computations it owns.\n\n## Import\n\n```ts\nimport { createRoot } from \"solid-js\";\n```\n\n## Type\n\n```ts\nfunction createRoot\u003cT\u003e(\n\tfn: (dispose: () =\u003e void) =\u003e T,\n\tdetachedOwner?: Owner\n): T;\n```\n\n## Parameters\n\n### `fn`\n\n- **Type:** `(dispose: () =\u003e void) =\u003e T`\n- **Required:** Yes\n\nThe function executes within a newly created owned context.\nThe computations created within this function are managed by the root and will only be disposed of when the provided `dispose` function is called.\n\nIf a function is passed without a `dispose` parameter, an unowned root is created.\nIn this case, the computations are not managed for disposal, which may lead to memory leaks.\n\nThis function itself does not track dependencies and only runs once.\n\n### `detachedOwner`\n\n- **Type:** `Owner`\n- **Required:** No\n\nAn optional owner that establishes the root's position in the ownership hierarchy.\nWhen provided, the root becomes owned by this owner and inherits its contextual state (such as [contexts](/concepts/context)).\n\n## Return Value\n\n`createRoot` returns the value returned by the `fn` function.\n\n## Examples\n\n### Basic Usage\n\n```ts\nimport { createSignal, createEffect, createRoot } from \"solid-js\";\n\nfunction createCounter(initial = 0) {\n\tconst [count, setCount] = createSignal(initial);\n\n\tcreateEffect(() =\u003e {\n\t\tconsole.log(`Count changed, new value: ${count()}`);\n\t});\n\n\tfunction increment() {\n\t\tsetCount((c) =\u003e c + 1);\n\t}\n\n\tfunction reset() {\n\t\tsetCount(initial);\n\t}\n\n\treturn { count, increment, reset };\n}\n\ntest(\"createCounter works correctly\", () =\u003e {\n\tcreateRoot((dispose) =\u003e {\n\t\tconst { count, increment, reset } = createCounter(10);\n\n\t\texpect(count()).toBe(10);\n\t\tincrement();\n\t\texpect(count()).toBe(11);\n\t\treset();\n\t\texpect(count()).toBe(10);\n\n\t\tdispose();\n\t});\n});\n```\n\n### Returning Values\n\n```ts\nimport { createRoot, createSignal, onCleanup } from \"solid-js\";\n\nconst counter = createRoot((dispose) =\u003e {\n\tconst [count, setCount] = createSignal(0);\n\n\tonCleanup(() =\u003e {\n\t\tconsole.log(\"Dispose was called!\");\n\t});\n\n\treturn {\n\t\tvalue: count,\n\t\tincrement: () =\u003e setCount((c) =\u003e c + 1),\n\t\tdispose,\n\t};\n});\n\nconsole.log(counter.value()); // 0\ncounter.increment();\nconsole.log(counter.value()); // 1\ncounter.dispose(); // Logs \"Dispose was called!\"\n```\n\n## Related\n\n- [`render`](/reference/rendering/render)",
  "url": "https://github.com/solidjs/solid-docs/blob/HEAD/src/routes/reference/reactive-utilities/create-root.mdx",
  "metadata": {
    "path": "src/routes/reference/reactive-utilities/create-root.mdx",
    "repo": "solidjs/solid-docs",
    "repo_url": "https://github.com/solidjs/solid-docs.git",
    "size": 2792,
    "source_type": "github"
  },
  "hash": "3fe1874fc54a969e96c787156eb0b6388617e93a6caf944d610883f9098db7e2",
  "timestamp": "2026-02-23T11:43:00.189839481+01:00"
}