[svelte] createTableHook: App* wrappers call setContext in the parent's component context (set_context_after_init when showing a column)
Les mainteneurs répondent en général sous 1 jour
Évaluation
- Difficulté
- 3/5
- Temps estimé
- 1-2 jours
- Accessibilité débutants
- 68/100
- Type d'issue
- Bug
- Clarté
- Clairement spécifiée
- Activité
- Active
- Stack technique
- typescript
- Domaine
- frontend
Piste de recherche
Start by reading createTableHook.svelte.js and the AppCell.svelte, AppHeader.svelte, and AppTable.svelte components named in the issue; check how the wrappers and components set context. Reproduce the error in the linked Svelte playground by showing the hidden Age column. Done means newly created cells, headers, and tables set context in their own component initialization, and toggling the column no longer errors.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Description
TanStack Table version
@tanstack/svelte-table v9.2.5 (the same code is in v9.2.6)
Framework/Library version
Svelte v5.57.1, with compilerOptions.experimental.async: true
Describe the bug and the steps to reproduce it
With createTableHook, any <table.AppCell> / <table.AppHeader> that is created after the component using it has initialized throws set_context_after_init, and the cells don't render. The most common trigger is showing a hidden column; it also affects anything else that creates new cells later.
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
at setContext
at AppCell (createTableHook.svelte.js)
Steps (see the playground below):
- Create a table with
createTableHook/createAppTablewhere one column is hidden (initialState: { columnVisibility: { age: false } }), and render cells with<table.AppCell {cell}>. - Show the column:
table.getColumn('age').toggleVisibility(). - The error is thrown and the new column's cells aren't rendered.
Cause: createAppTable builds the App* wrappers as plain functions that call setContext and then call the real component:
const AppCell = ((internal, { children, cell }) => {
setContext(cellContextKey, cell);
return AppCellSvelte(internal, { cell, cellComponents: cellComponentsWithFlexRender, children });
});
setContext runs before AppCellSvelte has pushed its own component context, so it writes into the parent's context (the component that renders <table.AppCell>). Svelte's async mode validates that setContext only runs while a component initializes. That holds during the parent's first render, but not for cells created later, e.g. when a column is shown. Without async mode it doesn't throw, but cells/headers still get written into the parent's context map rather than their own. The same applies to AppHeader, AppFooter and AppTable.
Possible fix, which I'm using as a local patch: let the App*Svelte components set the context themselves, during their own initialization, and have the wrappers only pass props:
// createTableHook.svelte.js
- const AppCell = ((internal, { children, cell }) => {
- setContext(cellContextKey, cell);
- return AppCellSvelte(internal, {
- cell,
- cellComponents: cellComponentsWithFlexRender,
- children,
- });
- });
+ const AppCell = ((internal, { children, cell }) => AppCellSvelte(internal, {
+ cell,
+ cellComponents: cellComponentsWithFlexRender,
+ contextKey: cellContextKey,
+ children,
+ }));
<!-- AppCell.svelte -->
<script lang="ts">
- import type { Snippet } from 'svelte'
+ import { setContext, type Snippet } from 'svelte'
...
- let { children, cell, cellComponents }: Props = $props()
+ let { children, cell, cellComponents, contextKey }: Props = $props()
+
+ setContext(contextKey, cell)
</script>
…and the same for AppHeader / AppFooter (AppHeader.svelte) and AppTable (AppTable.svelte, which would receive table as a prop). With that patch, showing/hiding columns, paging, sorting and filtering work without errors, and useCellContext / useHeaderContext / useTableContext return the right instances.
Your Minimal, Reproducible Example - (Sandbox Highly Recommended)
Click Toggle "Age": the error appears in the console and the "Age" cells don't render.
Screenshots or Videos (Optional)
No response
Do you intend to try to help solve this bug with your own PR?
Yes, I think I know how to fix it and will discuss it in the comments of this issue
Terms & Code of Conduct
- I agree to follow this project's Code of Conduct
- I understand that if my bug cannot be reliable reproduced in a debuggable environment, it will probably not be fixed and this issue may even be closed.
- Langage dominant
- TypeScript
- Étoiles
- 28.5k
- Forks
- 3.6k
- Merge moyen
- 17 h 39 min
- PR mergées (30 j)
- 10
Préparer son environnement
- Aucun Dockerfile ni fichier Docker Compose
- Propose un modèle de pull request
- Lire le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Autres issues de TanStack/table
-
Issue template renders `react-table`, not `TanStack/table`Peut-être pris @vishruthasgowda l’a pris il y a 16 jours. Ouverte
Difficulté 1/5 Moins d'une heure Accessibilité débutants 78/100
TanStack/table#6595 · 1 commentaire ·
Les mainteneurs répondent en général sous 1 jour
-
table-core 9.2.5+: private types leak from .d.ts files, declaration emit fails with TS2883Peut-être pris @MFA-G l’a pris il y a 1 jour. Ouverte
Difficulté 3/5 1-2 jours Accessibilité débutants 74/100
Les mainteneurs répondent en général sous 1 jour
-
Difficulté 4/5 3-5 jours Accessibilité débutants 38/100
TanStack/table#6601 · 2 commentaires ·
Les mainteneurs répondent en général sous 1 jour
-
Cast required to access custom options when defining a custom plugin/featurePeut-être pris @sorena-paydar l’a pris il y a 17 jours. Ouverte
Difficulté 5/5 Plus d'une semaine Accessibilité débutants 35/100
Les mainteneurs répondent en général sous 1 jour
-
`aggregatedCell` not typed against bound `cellComponents` in `createAppColumnHelper`Peut-être pris @cpruijsen l’a pris il y a 28 jours. Ouverte
Difficulté 3/5 1-2 jours Accessibilité débutants 78/100
Les mainteneurs répondent en général sous 1 jour
Toutes les issues de TanStack/table
Issues similaires
-
Difficulté 1/5 Moins d'une heure Accessibilité débutants 85/100
MystenLabs/MemWal#1163 · 2 commentaires ·
Les mainteneurs répondent en général sous 1 jour
-
Mondriaan
Difficulté 1/5 Moins d'une heure Accessibilité débutants 88/100
knaw-huc/textannoviz#709 ·
Les mainteneurs répondent en général sous 1 jour
-
billion-context-pi
Difficulté 2/5 1-3 heures Accessibilité débutants 62/100
ranxianglei/billion-context#2521 · 3 commentaires ·
Les mainteneurs répondent en général sous 1 jour
-
Add: YRF Music NepalOuvertestreams:add
Difficulté 1/5 Moins d'une heure Accessibilité débutants 62/100
Les mainteneurs répondent en général sous 1 jour
-
Difficulté 2/5 1-3 heures Accessibilité débutants 72/100