Skip to content
Esc

noctcore-react/enforce-context-consumption

A prop whose value a registered scoped context already supplies must be read from the context, not declared or destructured as a prop again.

Replacing a drilled bundle of props with a scoped context is a refactor that quietly unwinds. The next component that needs onDelete takes it as a prop, its parent threads it down from the context consumer above, and a few changes later the drilling is back with the context alongside it. Nothing fails: both paths deliver the same function.

This rule keeps the refactor done. You register each context once, with the files it applies to and the prop names it supplies; in those files, a props contract that declares one of those names again is reported.

Incorrect · src/board/TaskCard.tsx
// rule options: {"contexts":[{"hook":"useTaskActions","files":["**/board/**"],"providedProps":["onRun","onDelete"]}]}
// onRun is supplied by the board's TaskActions context
interface TaskCardProps {
task: Task;
onRun: (id: string) => void;
}
Correct · src/board/TaskCard.tsx
// rule options: {"contexts":[{"hook":"useTaskActions","files":["**/board/**"],"providedProps":["onRun","onDelete"]}]}
// the card reads the action from the context instead
interface TaskCardProps {
task: Task;
}

In a file matched by a registered context’s files, a name from that context’s providedProps:

  • as a member of an interface or object type alias whose name ends in Props (interface TaskCardProps { onRun: ... }, type RowProps = { onRun(): void });
  • as a property destructured from a function’s first parameter, when that parameter is annotated with a *Props type reference (function TaskCard({ onRun }: TaskCardProps)), in any function form.

The message names the hook to read the context with. When two entries supply the same name for a file, the first entry is named.

Incorrect · src/board/Column.tsx · 2 reports
// rule options: {"contexts":[{"hook":"useBoardChrome","files":["**/board/**"],"providedProps":["autoMode","concurrency"]}]}
// both values come from the board chrome context
export const Column = ({ autoMode, concurrency, title }: ColumnProps) => (
<ColumnHeader title={title} autoMode={autoMode} concurrency={concurrency} />
);
Correct · src/board/Column.tsx
// rule options: {"contexts":[{"hook":"useBoardChrome","files":["**/board/**"],"providedProps":["autoMode","concurrency"]}]}
// the header reads them itself
export const Column = ({ title }: ColumnProps) => <ColumnHeader title={title} />;
  • Anything, when contexts is empty: the registry is the whole signal.
  • Files outside an entry’s files. The composition root that renders the provider should not match.
  • A type whose name does not end in Props, such as the context’s own value type.
  • A parameter that is not annotated with a *Props type, a computed key, and members brought in through an intersection (type RowProps = BaseProps & {...}): only literal members are read.

A reusable leaf that legitimately takes one of the names as a prop (a generic button with onSelect) is the case to plan for. Either leave that name out of providedProps, or keep such leaves out of the entry’s files (or your config block’s ignores).

Option Type Default Meaning
contexts { hook, files, providedProps }[] [] The registered contexts.
contexts[].hook string required The hook that reads the context, named in the message.
contexts[].files string[] (globs) required Files where the context is available.
contexts[].providedProps string[] required The prop names the context supplies.
'noctcore-react/enforce-context-consumption': ['error', {
contexts: [
{
hook: 'useTaskActions',
files: ['src/components/board/**'],
providedProps: ['onRun', 'onCancel', 'onDelete', 'onMerge'],
},
{
hook: 'useWorktreesContext',
files: ['src/components/board/**'],
providedProps: ['worktrees', 'activeWorktree'],
},
],
}]

Without a scoped context that replaced drilled props, there is nothing to register. For general drilling, see no-prop-drilling.

  • no-prop-drilling reports props forwarded unchanged to a child; this rule reports a prop that a known context already supplies, forwarded or not.