{"library":"react-flatten-children","title":"React Children Flattener","description":"`react-flatten-children` is a lightweight React utility library designed to resolve challenges associated with React Fragments when processing component children. In React, fragments (`<></>` or `<React.Fragment>`) group multiple children but are treated as single children by their parent, which can disrupt components expecting direct access to all child elements (e.g., `react-router`'s `Switch` component looking for `Route` children). This package offers a single function, `flattenChildren`, which recursively traverses a component's `children` prop, extracts all valid React elements, and returns them as a flat array. This effectively \"unwraps\" fragments, ensuring all nested children are accessible. The current stable version is 1.1.2, with recent updates primarily adding TypeScript support in v1.1.0. Its primary value lies in simplifying child manipulation for library authors and application developers alike, preventing common issues where fragment usage can lead to unexpected component behavior or errors during child introspection. It helps maintain compatibility with user expectations of flexible fragment usage within component APIs.","language":"javascript","status":"active","last_verified":"Sun Apr 19","install":{"commands":["npm install react-flatten-children"],"cli":null},"imports":["import flattenChildren from 'react-flatten-children';","const flattenChildren = require('react-flatten-children');","import flattenChildren, { FlatChildren } from 'react-flatten-children';\n\nfunction MyComponent({ children }: { children: React.ReactNode }) {\n  const flatChildren: FlatChildren = flattenChildren(children);\n  // flatChildren is now typed as ReactElement[]\n}"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import React from 'react';\nimport { Switch as BaseSwitch, Route, Redirect } from 'react-router';\nimport flattenChildren from 'react-flatten-children';\n\n// Imagine these are your page components\nconst PublicHome = () => <div>Public Home</div>;\nconst PrivateHome = () => <div>Private Home</div>;\nconst Account = () => <div>Account Page</div>;\nconst Login = () => <div>Login Page</div>;\nconst About = () => <div>About Page</div>;\n\n// Create a fragment-ready Switch component that can handle nested fragments\nconst Switch = ({ children }) => (\n  <BaseSwitch>{flattenChildren(children)}</BaseSwitch>\n);\n\nconst Routes = ({ isLoggedIn }) => (\n  <Switch>\n    {isLoggedIn ? (\n      <>\n        <Route exact path=\"/\" component={PrivateHome} />\n        <Route path=\"/account\" component={Account} />\n      </>\n    ) : (\n      <>\n        <Route exact path=\"/\" component={PublicHome} />\n        <Route path=\"/login\" component={Login} />\n      </>\n    )}\n    <Route path=\"/about\" component={About} />\n    <Redirect to=\"/\" />\n  </Switch>\n);\n\nexport default Routes;","lang":"javascript","description":"This example demonstrates how to use `flattenChildren` to make a `react-router` Switch component compatible with React Fragments, allowing routes to be conditionally grouped within fragments.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}