mirror of
https://github.com/ionic-team/ionic-framework.git
synced 2025-08-20 12:29:55 +08:00
fix(react): eliminate use of deprecated findDOMNode
, resolves #20972
This commit is contained in:
@ -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', () => {
|
describe('when working with css classes', () => {
|
||||||
const myClass = 'my-class'
|
const myClass = 'my-class'
|
||||||
const myClass2 = 'my-class2'
|
const myClass2 = 'my-class2'
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
import { AnimationBuilder } from '@ionic/core';
|
import { AnimationBuilder } from '@ionic/core';
|
||||||
import React from 'react';
|
import React from 'react';
|
||||||
import ReactDom from 'react-dom';
|
|
||||||
|
|
||||||
import { NavContext } from '../contexts/NavContext';
|
import { NavContext } from '../contexts/NavContext';
|
||||||
import { RouterOptions } from '../models';
|
import { RouterOptions } from '../models';
|
||||||
@ -25,9 +24,13 @@ export const createReactComponent = <PropType, ElementType>(
|
|||||||
const displayName = dashToPascalCase(tagName);
|
const displayName = dashToPascalCase(tagName);
|
||||||
const ReactComponent = class extends React.Component<IonicReactInternalProps<PropType>> {
|
const ReactComponent = class extends React.Component<IonicReactInternalProps<PropType>> {
|
||||||
context!: React.ContextType<typeof NavContext>;
|
context!: React.ContextType<typeof NavContext>;
|
||||||
|
ref: React.RefObject<HTMLElement>;
|
||||||
|
|
||||||
constructor(props: IonicReactInternalProps<PropType>) {
|
constructor(props: IonicReactInternalProps<PropType>) {
|
||||||
super(props);
|
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() {
|
componentDidMount() {
|
||||||
@ -35,7 +38,9 @@ export const createReactComponent = <PropType, ElementType>(
|
|||||||
}
|
}
|
||||||
|
|
||||||
componentDidUpdate(prevProps: IonicReactInternalProps<PropType>) {
|
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);
|
attachProps(node, this.props, prevProps);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -64,7 +69,7 @@ export const createReactComponent = <PropType, ElementType>(
|
|||||||
|
|
||||||
const newProps: IonicReactInternalProps<PropType> = {
|
const newProps: IonicReactInternalProps<PropType> = {
|
||||||
...propsToPass,
|
...propsToPass,
|
||||||
ref: forwardedRef,
|
ref: forwardedRef || this.ref,
|
||||||
style
|
style
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -4,7 +4,12 @@ import App from './App';
|
|||||||
import './index.css';
|
import './index.css';
|
||||||
import * as serviceWorker from './serviceWorker';
|
import * as serviceWorker from './serviceWorker';
|
||||||
|
|
||||||
ReactDOM.render(<App />, document.getElementById('root'));
|
ReactDOM.render(
|
||||||
|
<React.StrictMode>
|
||||||
|
<App />
|
||||||
|
</React.StrictMode>,
|
||||||
|
document.getElementById('root')
|
||||||
|
);
|
||||||
|
|
||||||
// If you want your app to work offline and load faster, you can change
|
// If you want your app to work offline and load faster, you can change
|
||||||
// unregister() to register() below. Note this comes with some pitfalls.
|
// unregister() to register() below. Note this comes with some pitfalls.
|
||||||
|
Reference in New Issue
Block a user