{"record":{"id":"74375e15af3e4bc2","repo":"marmelab/react-admin","slug":"uselistsortcontext-must-be-used-inside-a-listsortc","errorCode":null,"errorMessage":"useListSortContext must be used inside a ListSortContextProvider","messagePattern":"useListSortContext must be used inside a ListSortContextProvider","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/ra-core/src/controller/list/useListSortContext.ts","lineNumber":18,"sourceCode":"import { useContext } from 'react';\n\nimport { ListSortContext, ListSortContextValue } from './ListSortContext';\n\n/**\n * Hook to read the list sort controller props from the ListSortContext.\n *\n * Must be used within a <ListSortContextProvider> (e.g. as a descendent of <List>\n * or <ListBase>).\n *\n * @returns {ListSortContextValue} list controller props\n *\n * @see useListController for how it is filled\n */\nexport const useListSortContext = (): ListSortContextValue => {\n    const context = useContext(ListSortContext);\n    if (!context) {\n        throw new Error(\n            'useListSortContext must be used inside a ListSortContextProvider'\n        );\n    }\n    return context;\n};\n","sourceCodeStart":1,"sourceCodeEnd":24,"githubUrl":"https://github.com/marmelab/react-admin/blob/051f511bb0afb5ea565c2d3728bf4dab0a6fa5e0/packages/ra-core/src/controller/list/useListSortContext.ts#L1-L24","documentation":"useListSortContext reads ListSortContext, which only exists under a ListSortContextProvider created by list controllers. Without it, the hook throws rather than returning undefined sort state.","triggerScenarios":"Calling useListSortContext in a component not nested under <List> or a manual <ListSortContextProvider value={{ sort, setSort }}>.","commonSituations":"Custom sort buttons/column headers rendered outside a list; unit tests missing the provider; reusing sort UI in non-list views.","solutions":["Nest the component inside a <List> page","Wrap with ListSortContextProvider in tests/stories providing sort/setSort","Use local useState for sort when no list is present"],"exampleFix":"// before\nexport const SortIndicator = () => {\n  const { sort } = useListSortContext(); // throws\n};\n// after\n<List>\n  <SortIndicator />\n</List>","handlingStrategy":"try-catch","validationCode":"const ctx = useContext(ListSortContext);\nif (!ctx) return <PlainHeader label=\"Title\" />;","typeGuard":"const hasSortContext = (): boolean => useContext(ListSortContext) !== null;","tryCatchPattern":"try { render(<SortableHeader field=\"title\" />); } catch (e) { if (String(e).includes('useListSortContext')) { /* nest inside List */ } }","preventionTips":["Use sort hooks only under <List>","Provide ListSortContextProvider in tests with sort/setSort","Use local state for sort in non-list components"],"tags":["react-admin","react-context","missing-provider"],"backgroundTag":"hook-called-outside-provider","analyzedSha":"051f511bb0afb5ea565c2d3728bf4dab0a6fa5e0","analyzedAt":"2026-08-30T02:28:14.926Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}