|
7 | 7 | // eslint-disable-next-line import/no-extraneous-dependencies
|
8 | 8 | import { Children } from 'react'
|
9 | 9 |
|
| 10 | +const defaultOptions = { |
| 11 | + componentWillUnmount: false, |
| 12 | +} |
| 13 | + |
10 | 14 | // Lifted from https://github.com/sindresorhus/p-reduce
|
11 | 15 | // Thanks @sindresorhus!
|
12 | 16 | const pReduce = (iterable, reducer, initVal) =>
|
@@ -49,7 +53,7 @@ export const isPromise = x => x != null && typeof x.then === 'function'
|
49 | 53 | // Recurse an React Element tree, running visitor on each element.
|
50 | 54 | // If visitor returns `false`, don't call the element's render function
|
51 | 55 | // or recurse into its child elements
|
52 |
| -export default function reactTreeWalker(element, visitor, context) { |
| 56 | +export default function reactTreeWalker(element, visitor, context, options = defaultOptions) { |
53 | 57 | return new Promise((resolve) => {
|
54 | 58 | const doVisit = (getChildren, visitorResult, childContext, isChildren) => {
|
55 | 59 | const doTraverse = (shouldContinue) => {
|
@@ -129,7 +133,21 @@ export default function reactTreeWalker(element, visitor, context) {
|
129 | 133 | instance.componentWillMount()
|
130 | 134 | }
|
131 | 135 |
|
132 |
| - return instance.render() |
| 136 | + const children = instance.render() |
| 137 | + |
| 138 | + if (options.componentWillUnmount && instance.componentWillUnmount) { |
| 139 | + try { |
| 140 | + instance.componentWillUnmount() |
| 141 | + } catch (err) { |
| 142 | + // This is an experimental feature, we don't want to break |
| 143 | + // the bootstrapping process, but lets warn the user it |
| 144 | + // occurred. |
| 145 | + console.warn('Error calling componentWillUnmount whilst walking your react tree') |
| 146 | + console.warn(err) |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + return children |
133 | 151 | },
|
134 | 152 | visitor(element, instance, context),
|
135 | 153 | () =>
|
|
0 commit comments