Skip to content

Commit 968a2ef

Browse files
Fix nested options dispose in strict mode (#825)
1 parent 2a107f8 commit 968a2ef

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

packages/devextreme-react/src/core/__tests__/component.test.tsx

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ describe('rendering', () => {
6262
</TestComponent>
6363
);
6464
const { container, unmount, rerender } = render(
65-
component, { legacyRoot: true }
65+
component, { legacyRoot: true },
6666
);
6767

6868
unmount();
@@ -171,6 +171,17 @@ describe('rendering', () => {
171171
expect(WidgetClass.mock.instances[1]).toEqual({});
172172
});
173173

174+
it('clears nested option in strict mode', () => {
175+
render(
176+
<React.StrictMode>
177+
<TestComponent>
178+
<TestComponent />
179+
</TestComponent>
180+
</React.StrictMode>,
181+
);
182+
expect(Widget.clearExtensions).toHaveBeenCalledTimes(4);
183+
});
184+
174185
it('do not pass children to options', () => {
175186
render(
176187
<TestComponent>
@@ -185,8 +196,8 @@ describe('rendering', () => {
185196
describe('element attrs management', () => {
186197
it('passes id, className and style to element', () => {
187198
const { container } = render(
188-
<TestComponent id="id1" className="class1" style={{ background: 'red' }} />, {
189-
},
199+
<TestComponent id="id1" className="class1" style={{ background: 'red' }} />,
200+
{},
190201
);
191202

192203
const element: HTMLElement = container.firstChild as HTMLElement;

packages/devextreme-react/src/core/__tests__/test-component.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const Widget = {
1818
off: (event: string, handler: (e: any) => void) => {
1919
eventHandlers[event] = eventHandlers[event].filter((e) => e !== handler);
2020
},
21+
clearExtensions: jest.fn(),
2122
dispose: jest.fn(),
2223
skipOptionsRollBack: false,
2324
};
@@ -36,6 +37,11 @@ class TestComponent<P = any> extends Component<P> {
3637
super._createWidget(element);
3738
Widget.option.mockReset();
3839
}
40+
41+
clearExtensions(): void {
42+
super.clearExtensions();
43+
Widget.clearExtensions();
44+
}
3945
}
4046
class TestPortalComponent<P = any> extends TestComponent<P> {
4147
protected isPortalComponent = true;

packages/devextreme-react/src/core/component.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,15 @@ class Component<P> extends ComponentBase<P> {
1818
this._createExtensions();
1919
}
2020

21+
public componentWillUnmount(): void {
22+
super.componentWillUnmount();
23+
this.clearExtensions();
24+
}
25+
26+
public clearExtensions(): void {
27+
this._extensionCreators = [];
28+
}
29+
2130
protected renderChildren(): Record<string, unknown>[] | null | undefined {
2231
return React.Children.map(
2332
this.props.children,

0 commit comments

Comments
 (0)