Behavior Props: Customizing Logic Flow
What you'll learn: How to expose props that let users customize your component's internal decision-making logic like filtering, sorting, or conditional rendering.
The Core Idea
Behavior props are function props that control how your component makes decisions, not just what it renders. Instead of hardcoding logic like "filter items where status is active" or "sort alphabetically," you expose props like filterFn, sortBy, or shouldRender that accept functions from the user.
This is inversion of control applied to logic flow: you provide the structure and execution, but users inject their own decision-making algorithms.
Why This Matters
Imagine a <DataTable> component. You could hardcode filtering:
// Rigid approach
<DataTable data={users} showActiveOnly />
But what if users need different filters? Behavior props solve this:
// Flexible approach
<DataTable
data={users}
filterFn={(user) => user.role === 'admin' && user.active}
/>
Now users control the filtering logic while your component handles the rendering.
Common Behavior Props
filterFn: Determines which items to includesortBy: Custom sorting algorithmshouldShow: Conditional rendering logicvalidateFn: Custom validation rulestransformData: Data preprocessing before rendering
The Pattern
You provide default behavior but accept function props that override it:
function List({ items, filterFn = () => true, sortBy }) {
let filtered = items.filter(filterFn);
if (sortBy) filtered = filtered.sort(sortBy);
return <ul>{filtered.map(/* render */)}</ul>;
}
Key Takeaway: Behavior props expose the decision points in your component's logic as customizable functions, giving users fine-grained control over how your component behaves without rebuilding it.