Skip to content

Commit 6cb0e77

Browse files
committed
Initial version.
1 parent 0f9718c commit 6cb0e77

File tree

18 files changed

+336
-3320
lines changed

18 files changed

+336
-3320
lines changed

README.md

Lines changed: 74 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,89 @@
1-
> This is my personal starter kit for npm libraries.
1+
# react-tree-walker 🌲
22

3-
# Your Library Name
3+
Walk a React element tree, executing a provided visitor function against each element.
44

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-
[![npm](https://img.shields.io/npm/v/npm-library-starter.svg?style=flat-square)](http://npm.im/npm-library-starter)
10-
[![MIT License](https://img.shields.io/npm/l/npm-library-starter.svg?style=flat-square)](http://opensource.org/licenses/MIT)
11-
[![Travis](https://img.shields.io/travis/ctrlplusb/npm-library-starter.svg?style=flat-square)](https://travis-ci.org/ctrlplusb/npm-library-starter)
12-
[![Codecov](https://img.shields.io/codecov/c/github/ctrlplusb/npm-library-starter.svg?style=flat-square)](https://codecov.io/github/ctrlplusb/npm-library-starter)
5+
[![npm](https://img.shields.io/npm/v/react-tree-walker.svg?style=flat-square)](http://npm.im/react-tree-walker)
6+
[![MIT License](https://img.shields.io/npm/l/react-tree-walker.svg?style=flat-square)](http://opensource.org/licenses/MIT)
7+
[![Travis](https://img.shields.io/travis/ctrlplusb/react-tree-walker.svg?style=flat-square)](https://travis-ci.org/ctrlplusb/react-tree-walker)
8+
[![Codecov](https://img.shields.io/codecov/c/github/ctrlplusb/react-tree-walker.svg?style=flat-square)](https://codecov.io/github/ctrlplusb/react-tree-walker)
139

1410
## TOCs
1511

1612
- [Introduction](#introduction)
13+
- [Example](#example)
1714
- [FAQs](#faqs)
1815

1916
## Introduction
2017

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.
2219

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+
}
2434

25-
___A common question around your library?___
35+
getValue() {
36+
return this.props.value;
37+
}
2638

27-
The answer to the question.
39+
render() {
40+
return <div>{this.props.children}</div>;
41+
}
42+
}
2843

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
3088

31-
The answer to the question.
89+
> Let me know if you have any...

examples/.eslintrc

Lines changed: 0 additions & 5 deletions
This file was deleted.

examples/web/.babelrc

Lines changed: 0 additions & 7 deletions
This file was deleted.

examples/web/package.json

Lines changed: 0 additions & 29 deletions
This file was deleted.

examples/web/server.js

Lines changed: 0 additions & 25 deletions
This file was deleted.

examples/web/src/index.js

Lines changed: 0 additions & 8 deletions
This file was deleted.

examples/web/tools/webpack/config.js

Lines changed: 0 additions & 41 deletions
This file was deleted.

examples/web/tools/webpack/html.js

Lines changed: 0 additions & 18 deletions
This file was deleted.

0 commit comments

Comments
 (0)