Skip to content

Commit 4dede70

Browse files
committed
Fix deprecated findDOMNode usage in React 19
Update the library to remove the use of deprecated `findDOMNode` and replace it with a ref-based approach. * **NonStrictPanel.tsx** - Replace `findDOMNode` with `React.createRef` to access the DOM element. - Update the `nativeElement` getter to return the current ref. - Modify the `render` method to clone the element with the ref. * **ReactElementProvider.ts** - Update the constructor to accept a ref instead of a `StrictPanel` or `NonStrictPanel`. - Update the `element` getter to return the current ref. - Modify the `show` and `hide` methods to use the ref. * **Flicking.tsx** - Remove the `useFindDOMNode` prop and use a ref-based approach. - Update the `_createPanelRefs` method to use `React.createRef`. - Update the `_getPanels` method to use the new ref-based approach. * **ReactRenderer.ts** - Update the `_collectPanels` method to use the new ref-based approach. - Update the `_createPanel` method to use the new ref-based approach. * **package-lock.json** - Update the version to `4.12.0-snapshot`.
1 parent 293da58 commit 4dede70

File tree

5 files changed

+21
-17
lines changed

5 files changed

+21
-17
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,20 @@
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; }
1313

1414
public render() {
1515
return this._hide
1616
? <></>
17-
: this.props.children;
17+
: React.cloneElement(React.Children.only(this.props.children) as React.ReactElement, {
18+
ref: this._elRef
19+
});
1820
}
1921

2022
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(elRef: React.RefObject<HTMLElement>) {
16+
this._elRef = 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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,13 @@ class ReactRenderer extends ExternalRenderer {
6666
protected _collectPanels() {
6767
const flicking = getFlickingAttached(this._flicking);
6868
const reactFlicking = this._reactFlicking;
69-
const reactPanels = reactFlicking.reactPanels;
69+
const reactPanels = reactFlicking.reactPanels.map(panel => panel.nativeElement);
7070

7171
this._panels = this._strategy.collectPanels(flicking, reactPanels);
7272
}
7373

7474
protected _createPanel(externalComponent: StrictPanel | NonStrictPanel | HTMLDivElement, options: PanelOptions) {
75-
return this._strategy.createPanel(externalComponent, options);
75+
return this._strategy.createPanel(externalComponent.nativeElement, options);
7676
}
7777
}
7878

0 commit comments

Comments
 (0)