Code Monkey home page Code Monkey logo

react-tree-walker's Introduction

react-tree-walker ๐ŸŒฒ

Walk a React element tree, executing a provided visitor function against each element.

npm MIT License Travis Codecov

TOCs

Introduction

This is a extract of the implementation within the awesome react-apollo project. I've come to find many use-cases for it in my own projects and want to avoid code duplication.

With this you could, for example, perform pre-rendering parses on your React element tree to do things like data prefetching. ๐Ÿค›

Example

In the below example we walk the tree and execute the getValue function on every element instance that has the function available. We then push the value into a values array.

import reactTreeWalker from 'react-tree-walker';

class Foo extends React.Component {
  constructor(props) {
    super(props);
    this.getValue = this.getValue.bind(this);
  }

  getValue() {
    return this.props.value;
  }

  render() {
    return <div>{this.props.children}</div>;
  }
}

const app = (
  <div>
    <h1>Hello World!</h1>
    <Foo value={1} />
    <Foo value={2}>
      <Foo value={4}>
        <Foo value={5} />
      </Foo>
    </Foo>
    <Foo value={3} />
  </div>
);

const values = [];

/**
 * Visitor to be executed on each element being walked.
 *
 * @param  element - The current element being walked.
 * @param  instance - If the current element is a Component or PureComponent
 *                    then this will hold the reference to the created
 *                    instance. For any other element type this will be null.
 * @param  context - The current "React Context". Any provided childContexTypes
 *                   will be passed down the tree.
 *
 * @return `undefined` if you want to continue walking down the current branch,
 *         or return `false` if you wish to stop the traversal down the
 *         current branch.  Stopping the traversal can be quite handy if
 *         you want to resolve a Promise for example.  You can wait for the
 *         Promise to resolve and then execute a function to continue
 *         traversal of the branch where you left off.
 */
function visitor(element, instance, context) {
  if (instance && typeof instance.getValue) {
    values.push(instance.getValue());
  }
};

reactTreeWalker(app, visitor);

console.log(values); // [1, 2, 4, 5, 3];

FAQs

Let me know if you have any...

react-tree-walker's People

Contributors

ctrlplusb avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.