mirror of
https://github.com/ionic-team/ionic-framework.git
synced 2025-08-19 19:57:22 +08:00
fix(react): eliminate use of deprecated findDOMNode
, resolves #20972
This commit is contained in:
@ -1,6 +1,5 @@
|
||||
import { AnimationBuilder } from '@ionic/core';
|
||||
import React from 'react';
|
||||
import ReactDom from 'react-dom';
|
||||
|
||||
import { NavContext } from '../contexts/NavContext';
|
||||
import { RouterOptions } from '../models';
|
||||
@ -25,9 +24,13 @@ export const createReactComponent = <PropType, ElementType>(
|
||||
const displayName = dashToPascalCase(tagName);
|
||||
const ReactComponent = class extends React.Component<IonicReactInternalProps<PropType>> {
|
||||
context!: React.ContextType<typeof NavContext>;
|
||||
ref: React.RefObject<HTMLElement>;
|
||||
|
||||
constructor(props: IonicReactInternalProps<PropType>) {
|
||||
super(props);
|
||||
// If we weren't given a ref to forward, we still need one
|
||||
// in order to attach props to the wrapped element.
|
||||
this.ref = React.createRef();
|
||||
}
|
||||
|
||||
componentDidMount() {
|
||||
@ -35,7 +38,9 @@ export const createReactComponent = <PropType, ElementType>(
|
||||
}
|
||||
|
||||
componentDidUpdate(prevProps: IonicReactInternalProps<PropType>) {
|
||||
const node = ReactDom.findDOMNode(this) as HTMLElement;
|
||||
// Try to use the forwarded ref to get the child node.
|
||||
// Otherwise, use the one we created.
|
||||
const node = (this.props.forwardedRef || this.ref.current!) as HTMLElement;
|
||||
attachProps(node, this.props, prevProps);
|
||||
}
|
||||
|
||||
@ -64,7 +69,7 @@ export const createReactComponent = <PropType, ElementType>(
|
||||
|
||||
const newProps: IonicReactInternalProps<PropType> = {
|
||||
...propsToPass,
|
||||
ref: forwardedRef,
|
||||
ref: forwardedRef || this.ref,
|
||||
style
|
||||
};
|
||||
|
||||
|
Reference in New Issue
Block a user