Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
react-keyed-flatten-children
Advanced tools
Flattens React children and fragments to an array with predictable and stable keys
Similar to React's built-in Children.toArray
method, this utility takes children and returns them as an array for introspection or filtering. Different from Children.toArray
, it will flatten arrays and React.Fragment
s into a regular, one-dimensional array while ensuring element and fragment keys are preserved, unique, and stable between renders.
npm install react-keyed-flatten-children
yarn add react-keyed-flatten-children
From the documentation of Children.toArray:
[toArray] returns the children opaque data structure as a flat array with keys assigned to each child. Useful if you want to manipulate collections of children in your render methods, especially if you want to reorder or slice this.props.children before passing it down.
Unfortunately it has some thorny edges:
Some have proposed, soon after Fragments were introduced, that a built-in React.Children.toFlatArray
would be useful, but
View the codesandbox here to get hands-on with how and when to utilise this module.
I've written a more application-focussed explanation in my article "Addressing Children.toArray's thorny edges".
In most cases react-keyed-flatten-children
is a drop-in replacement for Children.toArray
.
import flattenChildren from "react-keyed-flatten-children";
const MenuList = ({ children }) => {
const [selectedKey, setSelectedKey] = useState(null);
return (
<div role="menu">
{flattenChildren(props.children).map(child => {
if (child.type === MenuItem) {
return React.cloneElement(child, {
selected: child.key === selectedKey,
onClick: () => setSelectedKey(child.key)
});
}
return child;
})}
</div>
);
};
Now consumers can use arrays, fragments, or conditionally render items and your library will continue to work predictably.
<MenuList>
<h2>Animals</h2>
<MenuItem>Dogs</MenuItem>
<MenuItem>Cats</MenuItem>
<h2>Cars</h2>
{CARS_ARRAY.map(car => (
<MenuItem>{car}</MenuItem>
))}
{isLoggedIn() && (
<>
<h2>User</h2>
<MenuItem>You!</MenuItem>
<MenuItem>Someone else!</MenuItem>
</>
)
</MenuList>
Work around libraries which don't support fragments passed into children.
import flattenChildren from "react-keyed-flatten-children";
import { Switch, Route } from "react-router";
// A <Switch> looks through its children <Routes>, but won't match <Routes> within fragments.
// <FlexibleSwitch> will flatten out its children so <Switch> is able to see all children.
const FlexibleSwitch = ({ children }) => (
<Switch>{flattenChildren(children)}</Switch>
);
const AppRoutes = ({ user }) => (
<Router>
<GlobalNavigation user={user} />
<Switch>
<Route path="/about">
<About />
</Route>
{user && (
<>
<Route path="/users">
<Users />
</Route>
<Route path="/settings">
<Settings />
</Route>
</>
)}
<Route path="/">
<Home />
</Route>
</Switch>
</Router>
);
MIT
FAQs
Flattens React children and fragments to an array with predictable and stable keys
The npm package react-keyed-flatten-children receives a total of 161,275 weekly downloads. As such, react-keyed-flatten-children popularity was classified as popular.
We found that react-keyed-flatten-children demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.