Skip to content

Commit ff0eb08

Browse files
committed
fix(translate): fixes missing re-render on props.translate change
* Moves derived d3 state into actual `this.state`
1 parent 2e94772 commit ff0eb08

File tree

1 file changed

+14
-17
lines changed

1 file changed

+14
-17
lines changed

src/Tree/index.js

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class Tree extends React.Component {
1616
// eslint-disable-next-line react/no-unused-state
1717
dataRef: this.props.data,
1818
data: Tree.assignInternalProperties(clone(this.props.data)),
19+
d3: Tree.calculateD3Geometry(this.props),
1920
rd3tSvgClassName: `_${uuid.v4()}`,
2021
rd3tGClassName: `_${uuid.v4()}`,
2122
};
@@ -24,10 +25,6 @@ class Tree extends React.Component {
2425
initialRender: true,
2526
targetNode: null,
2627
isTransitioning: false,
27-
d3: {
28-
scale: this.props.zoom,
29-
translate: this.props.translate,
30-
},
3128
};
3229

3330
static getDerivedStateFromProps(nextProps, prevState) {
@@ -40,12 +37,13 @@ class Tree extends React.Component {
4037
};
4138
}
4239

43-
return null;
44-
}
40+
if (!deepEqual(Tree.calculateD3Geometry(nextProps), prevState.d3)) {
41+
return {
42+
d3: Tree.calculateD3Geometry(nextProps),
43+
};
44+
}
4545

46-
constructor(props) {
47-
super(props);
48-
this.internalState.d3 = Tree.calculateD3Geometry(this.props);
46+
return null;
4947
}
5048

5149
componentDidMount() {
@@ -68,13 +66,12 @@ class Tree extends React.Component {
6866
if (typeof this.props.onUpdate === 'function') {
6967
this.props.onUpdate({
7068
node: this.internalState.targetNode ? clone(this.internalState.targetNode) : null,
71-
zoom: this.internalState.d3.scale,
72-
translate: this.internalState.d3.translate,
69+
zoom: this.state.d3.scale,
70+
translate: this.state.d3.translate,
7371
});
74-
75-
this.internalState.d3 = Tree.calculateD3Geometry(this.props);
76-
this.internalState.targetNode = null;
7772
}
73+
// Reset the last target node after we've flushed it to `onUpdate`.
74+
this.internalState.targetNode = null;
7875
}
7976

8077
/**
@@ -120,8 +117,8 @@ class Tree extends React.Component {
120117
zoom: event.scale,
121118
translate: { x: event.translate[0], y: event.translate[1] },
122119
});
123-
this.internalState.d3.scale = event.scale;
124-
this.internalState.d3.translate = { x: event.translate[0], y: event.translate[1] };
120+
this.state.d3.scale = event.scale;
121+
this.state.d3.translate = { x: event.translate[0], y: event.translate[1] };
125122
}
126123
})
127124
// Offset so that first pan and zoom does not jump back to [0,0] coords
@@ -501,7 +498,7 @@ class Tree extends React.Component {
501498
allowForeignObjects,
502499
styles,
503500
} = this.props;
504-
const { translate, scale } = this.internalState.d3;
501+
const { translate, scale } = this.state.d3;
505502
const subscriptions = { ...nodeSize, ...separation, depthFactor, initialDepth };
506503
return (
507504
<div className={`rd3t-tree-container ${zoomable ? 'rd3t-grabbable' : undefined}`}>

0 commit comments

Comments
 (0)