|
1 | | -> This is my personal starter kit for npm libraries. |
| 1 | +# react-tree-walker 🌲 |
2 | 2 |
|
3 | | -# Your Library Name |
| 3 | +Walk a React element tree, executing a provided visitor function against each element. |
4 | 4 |
|
5 | | -A one liner description of your library. |
6 | | - |
7 | | -___BADGES: EDIT THESE TO USE THE URLS FOR EACH SERVICE CONFIGURED FOR YOUR OWN LIBRARY, THEN REMOVE THIS MESSAGE___ |
8 | | - |
9 | | -[](http://npm.im/npm-library-starter) |
10 | | -[](http://opensource.org/licenses/MIT) |
11 | | -[](https://travis-ci.org/ctrlplusb/npm-library-starter) |
12 | | -[](https://codecov.io/github/ctrlplusb/npm-library-starter) |
| 5 | +[](http://npm.im/react-tree-walker) |
| 6 | +[](http://opensource.org/licenses/MIT) |
| 7 | +[](https://travis-ci.org/ctrlplusb/react-tree-walker) |
| 8 | +[](https://codecov.io/github/ctrlplusb/react-tree-walker) |
13 | 9 |
|
14 | 10 | ## TOCs |
15 | 11 |
|
16 | 12 | - [Introduction](#introduction) |
| 13 | + - [Example](#example) |
17 | 14 | - [FAQs](#faqs) |
18 | 15 |
|
19 | 16 | ## Introduction |
20 | 17 |
|
21 | | -An introduction to your library... |
| 18 | +This is a port/generalisation of the implementation within the awesome [`react-apollo`](https://github.com/apollostack/react-apollo) project. I've come to find many use-cases in my own projects and want to avoid code duplication. |
22 | 19 |
|
23 | | -## FAQs |
| 20 | +It's quite useful for performing pre-rendering parses on your React element tree to do things like data prefetching. |
| 21 | + |
| 22 | +# Example |
| 23 | + |
| 24 | +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. |
| 25 | + |
| 26 | +```jsx |
| 27 | +import reactTreeWalker from 'react-tree-walker'; |
| 28 | + |
| 29 | +class Foo extends React.Component { |
| 30 | + constructor(props) { |
| 31 | + super(props); |
| 32 | + this.getValue = this.getValue.bind(this); |
| 33 | + } |
24 | 34 |
|
25 | | -___A common question around your library?___ |
| 35 | + getValue() { |
| 36 | + return this.props.value; |
| 37 | + } |
26 | 38 |
|
27 | | -The answer to the question. |
| 39 | + render() { |
| 40 | + return <div>{this.props.children}</div>; |
| 41 | + } |
| 42 | +} |
28 | 43 |
|
29 | | -___A common question around your library?___ |
| 44 | +const app = ( |
| 45 | + <div> |
| 46 | + <h1>Hello World!</h1> |
| 47 | + <Foo value={1} /> |
| 48 | + <Foo value={2}> |
| 49 | + <Foo value={4}> |
| 50 | + <Foo value={5} /> |
| 51 | + </Foo> |
| 52 | + </Foo> |
| 53 | + <Foo value={3} /> |
| 54 | + </div> |
| 55 | +); |
| 56 | + |
| 57 | +const values = []; |
| 58 | + |
| 59 | +/** |
| 60 | + * Visitor to be executed on each element being walked. |
| 61 | + * |
| 62 | + * @param element - The current element being walked. |
| 63 | + * @param instance - If the current element is a Component or PureComponent |
| 64 | + * then this will hold the reference to the created |
| 65 | + * instance. For any other element type this will be null. |
| 66 | + * @param context - The current "React Context". Any provided childContexTypes |
| 67 | + * will be passed down the tree. |
| 68 | + * |
| 69 | + * @return `undefined` if you want to continue walking down the current branch, |
| 70 | + * or return `false` if you wish to stop the traversal down the |
| 71 | + * current branch. Stopping the traversal can be quite handy if |
| 72 | + * you want to resolve a Promise for example. You can wait for the |
| 73 | + * Promise to resolve and then execute a function to continue |
| 74 | + * traversal of the branch where you left off. |
| 75 | + */ |
| 76 | +function visitor(element, instance, context) { |
| 77 | + if (instance && typeof instance.getValue) { |
| 78 | + values.push(instance.getValue()); |
| 79 | + } |
| 80 | +}; |
| 81 | + |
| 82 | +reactTreeWalker(tree, visitor); |
| 83 | + |
| 84 | +console.log(values); // [1, 2, 4, 5, 3]; |
| 85 | +``` |
| 86 | + |
| 87 | +## FAQs |
30 | 88 |
|
31 | | -The answer to the question. |
| 89 | +> Let me know if you have any... |
0 commit comments