You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A lightweight and fast control to render a select component that can display hierarchical tree data. In addition, the control shows the selection in pills and allows user to search the options for quick filtering and selection.
@@ -55,6 +53,7 @@ A lightweight and fast control to render a select component that can display hie
55
53
*[Virtualized rendering](#virtualized-rendering)
56
54
*[Reducing costly DOM manipulations](#reducing-costly-dom-manipulations)
57
55
*[FAQ](#faq)
56
+
*[Doing more with HOCs](/docs/HOC.md)
58
57
*[Development](#development)
59
58
*[License](#license)
60
59
@@ -97,36 +96,36 @@ In order to avoid version conflicts in your project, you must specify and instal
This documentation is about expanding react-dropdown-tree-select using Higher Order Components/Functions.
4
+
5
+
## Table of Contents
6
+
7
+
*[What are HOCs](#what-are-hocs)
8
+
*[Why use a HOC](#why-use-a-hoc)
9
+
*[Examples](#examples)
10
+
*[External Select All, Unselect All buttons](#external-select-all--unselect-all-buttons)
11
+
*[Internal Select All Checkbox](#internal-select-all-checkbox)
12
+
*[Prevent re-render on parent prop/state changes](#prevent-re-render-on-parent-prop-state-changes)
13
+
*[Tree Node Paths](#tree-node-paths)
14
+
15
+
## What are HOCs
16
+
17
+
HOCs (or Higher Order Components/Functions) are either a React Component (or a function that returns a React Component) that are used to enhance the functionality of an existing component.
18
+
19
+
You can use HOCs to manipulate the props and state, abstract rendering logic or enable code reuse.
20
+
21
+
## Why use a HOC
22
+
23
+
Or in other words, why is this functionality not baked in to the component? It's a fair question. There are many reasons but probably the biggest reason is - to _Keep It Simple_!
24
+
25
+
The control tries to provide a minimal, core set of features while making it easy to expand upon the core features using composition, or HOCs. This lets the component to have a very small footprint and allows you to customize/tweak or build upon as per your requirements.
26
+
27
+
## Examples
28
+
29
+
These are some of the examples of expanding the core component with HOCs. They are all provided as Code Sandboxes so feel free to play with them.
30
+
31
+
If you'd like to add to these examples, create an issue with a brief description (of what the HOC does) along with a CodeSandbox link.
32
+
33
+
### External Select All, Unselect All buttons
34
+
35
+
This is an example of selecting/unselecting all nodes programmatically, outside of the control.
36
+
37
+

### Prevent re-render on parent prop/state changes
50
+
51
+
Once initialized, the component (react-dropdown-tree-select) manages its own internal state. However, if this component is part of another component, then due to React's nature, anytime the parent is re-rendered, this component will re-render. While React's Virtual DOM diffing will cancel out any ultimate DOM modifications, it'll still go through all of its initialization process. This can be a waste of computation if the tree data hasn't changed.
52
+
53
+
To prevent that, this HOC simply adds a deep equality check in its `shouldComponentUpdate`.
54
+
55
+
This is not baked in the component since:
56
+
57
+
* Deep equality check can be expensive if the tree is large
58
+
* Not everyone may need this check
59
+
60
+
[](https://codesandbox.io/s/v05klkn56l)
61
+
62
+
### Tree Node Paths
63
+
64
+
The `onChange`/`onNodeToggle` events bubbles up few useful properties such as `_depth`, `_id`, `_parent` (and `_children` for non-leaf nodes). Using these, you can recurse up/down to grab a node in the tree.
65
+
66
+
In addition, you can use the object path notation to grab the node without recursion. This HOC demonstrates a technique to do that.
0 commit comments