Introducing PrimeReact v11-alpha 🎉Discover Now

useTreeFilter

Filter hook for hierarchical data with lenient and strict branch-matching semantics.

Overview#

useTreeFilter walks hierarchical data (nodes with children) and returns the matching branches. It shares the same rule shape and matcher registry as useFilter, so switching between flat and tree data is only a matter of which hook you call.

import { useTreeFilter } from '@primereact/hooks/use-tree-filter';

filterMode#

  • lenient (default) — a branch is kept if it matches directly or any descendant matches. The whole subtree comes along.
  • strict — leaves must match themselves; parents are kept only when a descendant matches. Non-matching leaves never appear.
Documents
Work Report.docx
Expenses.xlsx
Invoices
January.pdf
February.pdf
Media
Holiday.mp4
Wallpaper.jpg
Archive
old-work-report.docx

Lenient: a branch is kept if any descendant matches. Strict: leaves must match; parents are kept only when a descendant matches.

basic-demo

Rules#

Same shape as useFilter. For a node like { key, data: { name, type }, children }, field: 'name' resolves against node.data.name.

useTreeFilter({
    nodes,
    defaultRules: [{ field: 'name', value: 'doc', matchMode: 'contains' }],
    filterMode: 'lenient'
});

Lazy#

lazy: true skips the client-side tree walk; filteredNodes is returned as-is. onLazyLoad fires with the current rules plus the active filterMode — pass both to your backend so it honours the same branch-matching semantics.

useTreeFilter({
    nodes,
    lazy: true,
    defaultRules: [{ field: 'label', value: null }],
    filterMode: 'lenient',
    onLazyLoad: ({ rules, filterMode }) => {
        // fetch filtered tree from the server
    }
});

Custom matchers#

Use the same matchers option and the shared registerMatcher/unregisterMatcher helpers exposed from @primereact/hooks/use-filter.