{"record":{"id":"9c9ac1c759b29e99","repo":"marmelab/react-admin","slug":"cannot-change-the-order-on-an-undefined-sort-field","errorCode":null,"errorMessage":"cannot change the order on an undefined sort field","messagePattern":"cannot change the order on an undefined sort field","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/ra-core/src/controller/useSortState.ts","lineNumber":35,"sourceCode":"\nconst sortReducer = (state: SortPayload, action: Action): SortPayload => {\n    switch (action.type) {\n        case 'SET_SORT':\n            return action.payload;\n        case 'SET_SORT_FIELD': {\n            const field = action.payload;\n            const order =\n                state.field === field\n                    ? state.order === SORT_ASC\n                        ? SORT_DESC\n                        : SORT_ASC\n                    : SORT_ASC;\n            return { field, order };\n        }\n        case 'SET_SORT_ORDER': {\n            const order = action.payload;\n            if (!state.field) {\n                throw new Error(\n                    'cannot change the order on an undefined sort field'\n                );\n            }\n            return {\n                field: state.field,\n                order,\n            };\n        }\n        default:\n            return state;\n    }\n};\n\nexport const defaultSort = { field: '', order: 'ASC' } as const;\n\n/**\n * Set the sort { field, order }\n * @name setSort","sourceCodeStart":17,"sourceCodeEnd":53,"githubUrl":"https://github.com/marmelab/react-admin/blob/051f511bb0afb5ea565c2d3728bf4dab0a6fa5e0/packages/ra-core/src/controller/useSortState.ts#L17-L53","documentation":"The sortReducer handles SET_SORT_ORDER actions, which only toggle the order of an already-selected field. If the sort state has no field set, there is nothing to reorder, so the reducer throws rather than producing invalid state.","triggerScenarios":"Dispatching SET_SORT_ORDER (or calling setSort({ order: 'ASC' }) / changeSortOrder) while the current sort state is { field: '', order: ... } — i.e. toggling order before any sort field was set (e.g. a table header sort control that only flips order, used before clicking a column).","commonSituations":"Custom sort toggle buttons wired to setSortOrder but initialized without a field; clearing sort (field '') then pressing the direction toggle; misusing useSortState in custom UIs where the initial sort was never set.","solutions":["Dispatch SET_SORT_FIELD (or setSort with a full { field, order }) first instead of only setting the order","Guard the call: only toggle order when the current sort field is non-empty","Initialize the sort state with a default field, e.g. useSortState({ field: 'name', order: 'ASC' })"],"exampleFix":"// before\nonClick={() => setSort({ order: order === 'ASC' ? 'DESC' : 'ASC' })}\n// after\nonClick={() =>\n  sort.field\n    ? setSort({ field: sort.field, order: order === 'ASC' ? 'DESC' : 'ASC' })\n    : setSort({ field: 'name', order: 'ASC' })\n}","handlingStrategy":"validation","validationCode":"if (!sort.field) setSort({ field: DEFAULT_FIELD, order: 'ASC' }); else setSort({ field: sort.field, order: next });","typeGuard":"const hasSortField = (s: { field: string }) => typeof s.field === 'string' && s.field.length > 0;","tryCatchPattern":"try { setSort({ order: nextOrder }); } catch { setSort({ field: defaultField, order: nextOrder }); }","preventionTips":["Initialize useSortState with a default field","Use SET_SORT_FIELD before SET_SORT_ORDER","Never clear sort field while keeping order toggles active"],"tags":["react-admin","state","sort"],"backgroundTag":"invalid-state-transition","analyzedSha":"051f511bb0afb5ea565c2d3728bf4dab0a6fa5e0","analyzedAt":"2026-08-30T02:28:14.926Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}