Skip to content

Commit d0a2e9c

Browse files
committed
Allows state.data to update if props.data reference changes (#59)
1 parent 6edc780 commit d0a2e9c

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

src/Tree/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export default class Tree extends React.Component {
5555

5656
componentWillReceiveProps(nextProps) {
5757
// Clone new data & assign internal properties
58-
if (!deepEqual(this.props.data, nextProps.data)) {
58+
if (this.props.data !== nextProps.data) {
5959
this.setState({
6060
data: this.assignInternalProperties(clone(nextProps.data)),
6161
});

src/Tree/tests/index.test.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,18 +79,31 @@ describe('<Tree />', () => {
7979
data: mockData2,
8080
};
8181
const renderedComponent = mount(<Tree data={mockData} />);
82-
8382
expect(renderedComponent.instance().assignInternalProperties).toHaveBeenCalledTimes(
8483
mockDataDepth,
8584
);
86-
8785
renderedComponent.setProps(nextProps);
88-
8986
expect(renderedComponent.instance().assignInternalProperties).toHaveBeenCalledTimes(
9087
mockDataDepth + mockData2Depth,
9188
);
9289
});
9390

91+
it("reassigns internal props if `props.data`'s array reference changes", () => {
92+
// `assignInternalProperties` recurses by depth: 1 level -> 1 call
93+
const mockDataDepth = 2;
94+
const nextDataDepth = 2;
95+
const nextData = [...mockData];
96+
nextData[0].children.push({ name: `${nextData[0].children.length}` });
97+
const renderedComponent = mount(<Tree data={mockData} />);
98+
expect(renderedComponent.instance().assignInternalProperties).toHaveBeenCalledTimes(
99+
mockDataDepth,
100+
);
101+
renderedComponent.setProps({ data: nextData });
102+
expect(renderedComponent.instance().assignInternalProperties).toHaveBeenCalledTimes(
103+
mockDataDepth + nextDataDepth,
104+
);
105+
});
106+
94107
describe('translate', () => {
95108
it('applies the `translate` prop when specified', () => {
96109
const fixture = { x: 123, y: 321 };

0 commit comments

Comments
 (0)