1 | import React, {createElement} from 'react';
|
2 | import {findDOMNode, render} from 'react-dom';
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | export default function rerenderHOC(ComposedComponent, {captureNode} = {captureNode: true}) {
|
11 | return class Rerenderer extends ComposedComponent {
|
12 | _propsCache = {};
|
13 |
|
14 | onRefUpdate = component => {
|
15 |
|
16 | this.node = findDOMNode(component);
|
17 | };
|
18 |
|
19 | rerender(props = {}, callback) {
|
20 | let container;
|
21 |
|
22 | try {
|
23 | container = this.node.parentNode;
|
24 | } finally {
|
25 | if (!container) {
|
26 | throw new Error(`${this.constructor.name} component isn't mounted`);
|
27 | }
|
28 | }
|
29 |
|
30 | this._propsCache = Object.assign({}, this.props, this._propsCache, props);
|
31 |
|
32 | return render(createElement(this.constructor, this._propsCache), container, callback);
|
33 | }
|
34 |
|
35 | render() {
|
36 | if (!captureNode) {
|
37 | return super.render();
|
38 | }
|
39 |
|
40 | return (
|
41 | <ComposedComponent
|
42 | ref={this.onRefUpdate}
|
43 | {...this.props}
|
44 | />
|
45 | );
|
46 | }
|
47 | };
|
48 | }
|