npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@logicamente.info/react-traverse

v1.0.0

Published

React Nodes and Components Traversal

Downloads

3

Readme

react-traverse

react-traverse applies the principle of tree traversal to the two kinds of trees present in a React hierarchy: React nodes and React components.

Install

npm install @simple-contacts/react-traverse
yarn add @simple-contacts/react-traverse

React node traversal

traverse(node, visitor) transforms a React nodes hierarchy into another one (borrowing its syntax from babel). A React node is typically what is returned by a single components render function.

For example, you can replace all <div>s with <span>s:

const replaceDivsWithSpans = (node) => traverse(node, {
  DOMElement(path) {
    if(path.node.type === 'div') {
      return React.createElement(
        'span',
        path.node.props,
        ...path.traverseChildren(),
      );
    }
    return React.cloneElement(
      path.node,
      path.node.props,
      ...path.traverseChildren(),
    );
  },
});

replaceDivsWithSpans(<div>This is a span.</div>)
// will render as:
<span>This is a div.</span>

See the full traversal API below.

React components wrapping

traverse is notably useful to decorate custom components (either classes extending React.Component or stateless function components). So there is a simple decorator, wrapRender(transformNode)(component), which does exactly what it says on the tin.

For example, you can reuse replaceDivsWithSpans and wrap a component in it:

class Component extends React.Component {
  render() {
    return <div>This is a span.</div>;
  }
}

const WrappedComponent = wrapRender(replaceDivsWithSpans)(Component);
// <WrappedComponent /> will render as:
<span>This is a span.</span>

React components traversal

transformComponents(transformComponent) transforms a React components hierarchy into another one. Think higher-higher-order components, or decorators on steroids. A React Component is either a class extending React.Component or a stateless functional render function. Not only does it transform the component class you apply it two, but also recursively to all the subcomponents.

It combines very well with both traverse and wrapRender, as you can apply node transforms to the whole Virtual DOM tree, not only component-local parts of it.

For example, you can transform ALL the divs of your app into spans:

class Foo extends React.Component {
  render() {
    return <div>This is foo.</div>;
  }
}

function Bar() {
  return <div>
    This is bar.
    <Foo />
  </div>;
}

const TransformedBar = transformComponents(wrapRender(replaceDivsWithSpans))(Bar);

// <TransformedBar /> will render as:
<span>
  This is bar.
  <span>This is foo.</span>
</span>

For convenience, you can use transformComponents on components classes (created using extends React.Component), on stateless function components, or directly on React Elements:

const transform = transformComponents(wrapRender(replaceDivsWithSpans));
// decorator
@transform
class Foo extends React.Component { ... }

// stateless function
const Foo = transform(
  () => <div>This is foo.</div>
);

// directly on a ReactElement, eg. in a ReactDOM.render call
ReactDOM.render(transform(
  <div>
    <Foo />
  </div>
));

Node visitor

The following visitor policies are available:

  • Empty: null, undefined or boolean
  • Text: string or number
  • Fragment: array of React Nodes
  • DOMElement: non-component elements (div, span, etc)
  • ComponentElement: component elements

If a visitor policy is not provided for a given kind, it defaults to a reasonable behaviour:

  • Empty and Text return the original node
  • Fragment return a new array in which each node has been traversed
  • DOMElement and ComponentElement return a clone element with the same props, except the children which have also been traversed.

A visitor is passed a single object, path, which has the following properties:

  • path.node: the original node
  • path.kindOf(node): a function which returns the kind of node as a string (Empty, Text, etc)
  • path.traverse(node, visitor = path.visitor): a recursive call to the traversal function
  • path.traverseChildren(): a shortcut to traverse the children of path.node
  • path.visitor: the visitor object for the current traversal

transformComponents memoization

Calls to transformComponents(transformComponent)(component) are memoized using a WeakMap to avoid allocating zillions of closures every time the app is rendered. This assumes transformComponent itself is pure (stateless) and component is immutable. This should be the case unless you're doing something very wrong.

Tests

To test. yarn tests