fix(react): eliminate use of deprecated findDOMNode, resolves #20972

This commit is contained in:
Ely Lucas
2020-11-25 14:21:12 -07:00
committed by GitHub
parent ea52db66f0
commit 5275332e43
3 changed files with 29 additions and 4 deletions

View File

@ -49,6 +49,21 @@ describe('createComponent - ref', () => {
});
});
describe('createComponent - strict mode', () => {
beforeEach(() => (console.error = (...data: any[]) => {
throw new Error(...data);
}));
test('should work without errors in strict mode', () => {
const renderResult = render(
<React.StrictMode>
<IonButton>Strict Mode Rocks</IonButton>
</React.StrictMode>
);
expect(renderResult.getByText(/Rocks/)).toBeTruthy();
});
});
describe('when working with css classes', () => {
const myClass = 'my-class'
const myClass2 = 'my-class2'

View File

@ -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
};