mirror of
https://github.com/zhigang1992/react-navigation.git
synced 2026-04-29 04:45:19 +08:00
feat: add deprecation wanrnings
This commit is contained in:
64
packages/stack/src/utils/validateDeprecatedOptions.tsx
Normal file
64
packages/stack/src/utils/validateDeprecatedOptions.tsx
Normal file
@@ -0,0 +1,64 @@
|
||||
import { NavigationStackOptions } from '../types';
|
||||
|
||||
type Validation = {
|
||||
check: (o: NavigationStackOptions) => boolean;
|
||||
deprecated: string;
|
||||
updated: string;
|
||||
};
|
||||
|
||||
let shownWarnings: string[] = [];
|
||||
|
||||
const validations: Validation[] = [
|
||||
{
|
||||
check: o => o.header === null,
|
||||
deprecated: 'header: null',
|
||||
updated: 'headerShown: false',
|
||||
},
|
||||
{
|
||||
check: o => o.header != null && typeof o.header !== 'function',
|
||||
deprecated: 'header: <SomeElement />',
|
||||
updated: 'header: () => <SomeElement />',
|
||||
},
|
||||
{
|
||||
check: o =>
|
||||
o.headerTitle !== undefined &&
|
||||
typeof o.header !== 'string' &&
|
||||
typeof o.header !== 'function',
|
||||
deprecated: 'headerTitle: <SomeElement />',
|
||||
updated: 'headerTitle: () => <SomeElement />',
|
||||
},
|
||||
...['headerLeft', 'headerRight', 'headerBackground', 'backImage'].map(p => ({
|
||||
check: (o: any) => o[p] !== undefined && typeof o[p] !== 'function',
|
||||
deprecated: `${p}: <SomeElement />`,
|
||||
updated: `${p}: () => <SomeElement />`,
|
||||
})),
|
||||
];
|
||||
|
||||
export default function validateDeprecatedOptions(
|
||||
options: NavigationStackOptions
|
||||
) {
|
||||
const warnings: Validation[] = [];
|
||||
|
||||
// Validate options to show warnings for deprecations
|
||||
validations.forEach(v => {
|
||||
if (shownWarnings.includes(v.deprecated)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (v.check(options)) {
|
||||
warnings.push(v);
|
||||
shownWarnings.push(v.deprecated);
|
||||
}
|
||||
});
|
||||
|
||||
if (warnings.length) {
|
||||
console.warn(
|
||||
`Deprecation in 'navigationOptions':\n${warnings
|
||||
.map(
|
||||
v =>
|
||||
`- '${v.deprecated}' will be removed in a future version. Use '${v.updated}' instead`
|
||||
)
|
||||
.join('\n')}`
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -18,6 +18,7 @@ import {
|
||||
ModalTransition,
|
||||
} from '../../TransitionConfigs/TransitionPresets';
|
||||
import { forNoAnimation } from '../../TransitionConfigs/HeaderStyleInterpolators';
|
||||
import validateDeprecatedOptions from '../../utils/validateDeprecatedOptions';
|
||||
import {
|
||||
Layout,
|
||||
HeaderMode,
|
||||
@@ -344,6 +345,14 @@ export default class Stack extends React.Component<Props, State> {
|
||||
? 1
|
||||
: 0;
|
||||
|
||||
if (
|
||||
process.env.NODE_ENV !== 'production' &&
|
||||
scene.descriptor &&
|
||||
scene.descriptor.options
|
||||
) {
|
||||
validateDeprecatedOptions(scene.descriptor.options);
|
||||
}
|
||||
|
||||
const {
|
||||
header,
|
||||
headerShown,
|
||||
|
||||
Reference in New Issue
Block a user