Skip to content

Commit dab4a8c

Browse files
authored
fix: remove findDOMNode in react-flicking (#931)
* fix: remove findDOMNode in react-flicking * fix: fix error cases in ReactElementProvider
1 parent b30e560 commit dab4a8c

File tree

6 files changed

+26
-16
lines changed

6 files changed

+26
-16
lines changed

packages/react-flicking/src/react-flicking/Flicking.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -332,9 +332,7 @@ class Flicking extends React.Component<Partial<FlickingProps & FlickingOptions>>
332332
: getRenderingPanels(vanillaFlicking, diff(origChildren, origChildren))
333333
: origChildren;
334334

335-
return this.props.useFindDOMNode
336-
? children.map((child, idx) => <NonStrictPanel key={child.key!} ref={this._panels[idx] as any}>{child}</NonStrictPanel>)
337-
: children.map((child, idx) => <StrictPanel key={child.key!} ref={this._panels[idx] as any}>{child}</StrictPanel>)
335+
return children.map((child, idx) => <StrictPanel key={child.key!} ref={this._panels[idx] as any}>{child}</StrictPanel>);
338336
}
339337

340338
private _isFragment(child: React.ReactElement) {

packages/react-flicking/src/react-flicking/NonStrictPanel.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,21 @@
33
* egjs projects are licensed under the MIT license
44
*/
55
import * as React from "react";
6-
import { findDOMNode } from "react-dom";
76

87
class NonStrictPanel extends React.Component<{ children?: React.ReactElement }> {
98
private _hide: boolean = false;
9+
private _elRef: React.RefObject<HTMLElement> = React.createRef();
1010

11-
public get nativeElement() { return findDOMNode(this) as HTMLElement; }
11+
public get nativeElement() { return this._elRef.current!; }
1212
public get rendered() { return !this._hide; }
13+
public get elRef() { return this._elRef; }
1314

1415
public render() {
1516
return this._hide
1617
? <></>
17-
: this.props.children;
18+
: React.cloneElement(React.Children.only(this.props.children) as React.ReactElement, {
19+
ref: this._elRef
20+
});
1821
}
1922

2023
public show() {

packages/react-flicking/src/react-flicking/ReactElementProvider.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,25 @@ import StrictPanel from "./StrictPanel";
77
import NonStrictPanel from "./NonStrictPanel";
88

99
class ReactElementProvider implements ElementProvider {
10-
private _el: StrictPanel | NonStrictPanel;
10+
private _elRef: React.RefObject<HTMLElement>;
1111

12-
public get element() { return this._el.nativeElement; }
13-
public get rendered() { return this._el.rendered; }
12+
public get element() { return this._elRef.current!; }
13+
public get rendered() { return this._elRef.current != null; }
1414

15-
public constructor(el: StrictPanel | NonStrictPanel) {
16-
this._el = el;
15+
public constructor(el: StrictPanel | NonStrictPanel | HTMLDivElement) {
16+
this._elRef = el instanceof Element ? { current: el } : el.elRef;
1717
}
1818

1919
public show() {
20-
this._el.show();
20+
if (this._elRef.current) {
21+
this._elRef.current.style.display = "";
22+
}
2123
}
2224

2325
public hide() {
24-
this._el.hide();
26+
if (this._elRef.current) {
27+
this._elRef.current.style.display = "none";
28+
}
2529
}
2630
}
2731

packages/react-flicking/src/react-flicking/ReactRenderer.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,15 @@ class ReactRenderer extends ExternalRenderer {
6868
protected _collectPanels() {
6969
const flicking = getFlickingAttached(this._flicking);
7070
const reactFlicking = this._reactFlicking;
71-
const reactPanels = reactFlicking.reactPanels;
71+
const reactPanels = reactFlicking.reactPanels.map(panel => panel instanceof HTMLDivElement ? panel : panel.nativeElement);
7272

7373
this._panels = this._strategy.collectPanels(flicking, reactPanels);
7474
}
7575

7676
protected _createPanel(externalComponent: StrictPanel | NonStrictPanel | HTMLDivElement, options: PanelOptions) {
77-
return this._strategy.createPanel(externalComponent, options);
77+
const el = externalComponent instanceof HTMLDivElement ? externalComponent : externalComponent.nativeElement;
78+
79+
return this._strategy.createPanel(el, options);
7880
}
7981
}
8082

packages/react-flicking/src/react-flicking/StrictPanel.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class StrictPanel extends React.Component<{ children?: React.ReactElement }> {
1111

1212
public get nativeElement() { return this._elRef.current!; }
1313
public get rendered() { return !this._hide; }
14+
public get elRef() { return this._elRef; }
1415

1516
public render() {
1617
return this._hide

src/control/Control.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,9 @@ abstract class Control {
389389
return animate();
390390
} else {
391391
return animate().then(async () => {
392-
await flicking.renderer.render();
392+
if (flicking.initialized) {
393+
await flicking.renderer.render();
394+
}
393395
}).catch(err => {
394396
if (axesEvent && err instanceof FlickingError && err.code === ERROR.CODE.ANIMATION_INTERRUPTED) return;
395397
throw err;

0 commit comments

Comments
 (0)