udivankin / react-map-children

Map React component's children recursively

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

React-map-children

Iterates through given React component tree to gather some data

Install

Via npm:

npm install --save react-map-children

Usage

import React from 'react';
import mapRecursive from 'react-map-children';

const Route = ({ children, path }) => (
  <a href={path}>{children}</a>
);

const routes = (
  <Route path="/">
    <Route path="/index">Index</Route>
    <Route path="/about">About</Route>
    <Route path="/contacts">Contacts</Route>
  </Route>
);

const paths = mapRecursive(
  routes,
  route => (React.isValidElement(route) ? route.props.path : null),
).filter(Boolean);

console.log(paths); // ['/', 'index', 'about', 'contacts']

About

Map React component's children recursively


Languages

Language:JavaScript 100.0%