Utils to manage your React Children; find and filter children by type or custom function, enforce child content, and more!
Hello friend. Have you ever had the need to:
If you answered yes to any of those questions, then it sounds like your children could use a nanny to help bring order to the chaos...
Version: 2.15.0
react-nanny
doesn't have any dependencies. However, it does have a peer dependency of "react": ">=16.0.0"
which you most likely satisfy if you're the kind of person who's looking for utils for React children.
This is simple example of how you can program defensively (your consumer can't just throw anything unexpected in children and have it render) and it shows how you can manipulate your children to place them anywhere in the rendered output.
Below, we have a ToDo
list of Items
. We first get all child Items
—all other children will be ignored. We then find two lists of children that are completed and incomplete.
import React from 'react';
import { getChildrenByType, getChildren } from 'react-nanny';
import Item from './Item';
export const ToDoList ({ children }) => {
// Get all children of type Item
const items = getChildrenByType(children, [Item]);
// Find all incomplete and complete Items
const incomplete = getChildren(items, child => !child.props.completed);
const completed = getChildren(items, child => child.props.completed);
return (
<>
<div>
<h3>To Do</h3>
<ul>
{incomplete}
</ul>
</div>
<div>
<h3>Completed</h3>
<ul>
{completed}
</ul>
</div>
</>
);
};
Click on each function name for details and examples
function | Description |
---|---|
getChild | Gets first child by specified predicate |
getChildDeep | Gets first child by specified predicate (deep search) |
getChildByType | Gets first child by specified type |
getChildByTypeDeep | Gets first child by specified type (deep search) |
getChildren | Gets all children by specified predicate |
getChildrenDeep | Gets all children by specified predicate (deep search) |
getChildrenByType | Gets all children by specified type |
getChildrenByTypeDeep | Gets all children by specified type (deep search) |
getChildrenWithDescendant | Gets all children by specified predicate or that have a descendant node in their lineage which matches the predicate |
getChildrenWithDescendantByType | Gets all children by specified type or that have a descendant node in their lineage which match the specified type |
getDescendantDepth | Gets the depth to the first descendant (or self) of each root child that match the specified predicate |
getDescendantDepthByType | Gets the depth to the first descendant (or self) of each root child that match the specified types |
noEmptyChildrenDeep | Ensure that there is some level of content and not just a bunch of empty divs, spans, etc (deep search) |
overrideProps | Immutably override props of the children of the original component and (optionally) the original component |
overridePropsDeep | Immutably override props of the children and all descendants (deep) |
removeChildren | Removes all children by specified predicate |
removeChildrenDeep | Removes all children by specified predicate (deep search) |
removeChildrenByType | Removes all children by specified type |
removeChildrenByTypeDeep | Removes all children by specified type (deep search) |
typeOfComponent | Gets the string type of the component's {customTypeKey}, string type of the core html (JSX intrinsic) element, or the function type |
You can use an imported type, a React.ReactNode
, value from typeOfComponent
, a string type for an HTML (JSX Intrinsic) element, or a string representation of the type by using the customTypeKey
feature.
One simple way to be able to define and identify a type on a component and ensure that it is the same in development builds and production builds is to add a constant prop that contains the string type. Consider the following hypothetical component:
``` import React from 'react'; const Hello = ({ __TYPE }) =>The Hello
has a prop __TYPE
that has a value of 'Hello'
. We can query against this value and know that it's reliable regardless of environment.
The customTypeKey
in react-nanny
defines what the name of this prop is. In our example, customTypeKey
would be '__TYPE'
to query using this technique
Let's say you don't like __TYPE
and what to use your own value such as: CUSTOM
. You can accomplish this by providing the name for the customTypeKey
:
customTypeKey
, check out my Medium article: Find & Filter React Children By Type
Because React.forwardRef components are higher order components, determining their type becomes tricky. The only way to reliably determine their type is to use the customTypeKey
method outlined above.