Skip to content
Source
Sign inSign up
TanStack/table

fix(svelte-table): delegate context setting to App* components during initialization #6622

Closedgrzdev(GitHub profile, opens in a new tab)wants to merge 1 commit intomain fromrefs/pull/6622/headopened

Summary

In @tanstack/svelte-table, createAppTable previously defined AppTable, AppCell, AppHeader, and AppFooter wrappers as plain functions that invoked setContext() directly before invoking the underlying Svelte component (AppCellSvelte, AppHeaderSvelte, AppTableSvelte).

Because setContext was called in the wrapper function closure before the Svelte component mounted, it ran in the parent component's context. When cells or headers are rendered dynamically after the parent component has already initialized (for example, when toggling column visibility via column.toggleVisibility() or with Svelte 5 async mode enabled), Svelte throws:

Svelte error: set_context_after_init
`setContext` must be called when a component first initializes, not in a subsequent effect or after an `await` expression

Changes

  • Forward contextKey and the respective entity (cell, header, table) as props into AppCellSvelte, AppHeaderSvelte, and AppTableSvelte.
  • Invoke setContext inside the <script lang="ts"> of AppCell.svelte, AppHeader.svelte, and AppTable.svelte during their own component initialization.
  • Remove setContext calls from the outer wrapper function closure in createTableHook.svelte.ts.
  • Add changeset for @tanstack/svelte-table.

Summary by CodeRabbit

  • Bug Fixes
    • Fixed an initialization issue that could cause a Svelte context error when using table components. Context is now set during component initialization, while existing rendering behavior remains unchanged. This improves reliability for tables, cells, headers, and footers that rely on shared context.
    1. 🦋 Changeset detected

      Latest commit: 57041b272bfcd1773625bc76c008dbc0a2bc3002

      The changes in this PR will be included in the next version bump.

      This PR includes changesets to release 1 package
      NameType
      @tanstack/svelte-tablePatch

      Not sure what this means? Click here to learn what changesets are.

      Click here if you're a maintainer who wants to add another changeset to this PR

    2. coderabbitai[bot](GitHub profile, opens in a new tab)commented
      Review in Change Stack →

      No actionable comments were generated in the recent review. 🎉

      ℹ️ Recent review info
      ⚙️ Run configuration
      • Configuration used: Repository: TanStack/table/.coderabbit.yaml
      • Review profile: CHILL
      • Plan: Advanced
      • Run ID: 5b2487f2-1659-404b-8ef4-1d4b60d77939
      📥 Commits

      Reviewing files that changed from the base of the PR and between 6aa0d74946b393f4f316085998bb6ec31c2ef593 and 57041b272bfcd1773625bc76c008dbc0a2bc3002.

      📒 Files selected for processing (5)
      • .changeset/fix-svelte-table-app-context-init.md
      • packages/svelte-table/src/AppCell.svelte
      • packages/svelte-table/src/AppHeader.svelte
      • packages/svelte-table/src/AppTable.svelte
      • packages/svelte-table/src/createTableHook.svelte.ts

      Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 8 remain after this review.


      📝 Walkthrough

      Walkthrough

      The Svelte table wrappers now pass context keys and values to App components. Those components register the context with setContext during initialization.

      Changes

      Svelte context initialization

      Layer / File(s)Summary
      Register context in App components
      packages/svelte-table/src/AppCell.svelte, packages/svelte-table/src/AppHeader.svelte, packages/svelte-table/src/AppTable.svelte
      AppCell and AppHeader accept an optional contextKey and register their cell or header when it is provided. AppTable accepts an optional contextKey and table value, then registers the table when the key is provided.
      Pass context props from wrappers
      packages/svelte-table/src/createTableHook.svelte.ts, .changeset/fix-svelte-table-app-context-init.md
      The wrapper callbacks pass context keys to the App components instead of calling setContext. AppTable also receives the table instance. The AppFooter callback passes headerContextKey to AppHeaderSvelte. The changeset describes the initialization fix.

      Priority: ⬇️ Low

      Estimated code review effort: 2 (Simple) | ~10 minutes

      Change: Bug fix

      Suggested reviewers: kevinvandy

      Merge Risk: ⚪ Minimal · up to 57041

      The context change preserves the existing wrapper contracts while moving registration into component initialization. No concrete merge-blocking risk is evident.

      🚥 Pre-merge checks | ✅ 5
      ✅ Passed checks (5 passed)
      Check nameStatusExplanation
      Title check✅ PassedThe title clearly and concisely describes the main change: moving context setting to App* components during initialization.
      Description check✅ PassedThe description explains the problem, motivation, implementation, and changeset. It includes the main Changes content but omits the template's Checklist and Release Impact sections.
      Docstring Coverage✅ PassedDocstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 2 functions across 1 files. (4 skipped: 4 …
      Linked Issues check✅ PassedCheck skipped because no linked issues were found for this pull request.
      Out of Scope Changes check✅ PassedCheck skipped because no linked issues were found for this pull request.
      ✨ Finishing Touches
      🧪 Generate unit tests (beta)
      • Create a new PR
      • Autopilot · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

      Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

      ❤️ Share
      Comment `@coderabbitai help` to get the list of available commands.
    3. grzdev(GitHub profile, opens in a new tab)commented

      Closing in favor of the earlier open pull request https://github.com/TanStack/table/pull/6618.

Sign in to join this conversation.