javascriptreactjsreact-native

Warning: Page: Support for defaultProps will be removed from function components in a future major release


Warning: Page: Support for defaultProps will be removed from function components in a future major release. Use JavaScript default parameters instead.

here is my Page.js file

import { Dimensions, Text, View } from 'react-native';
import PropTypes from 'prop-types';
import React from 'react';
import { useTranslation } from 'react-i18next';

const Page = ({
  isLight,
  image,
  title,
  subtitle,
  width,
  height,
  containerStyles,
  imageContainerStyles,
  allowFontScaling,
  titleStyles,
  subTitleStyles,
}) => {
  let titleElement = title;
  const {t} = useTranslation()
  if (typeof title === 'string' || title instanceof String) {
    titleElement = (
      <View style={styles.padding}>
        <Text allowFontScaling={allowFontScaling} style={[styles.title, isLight ? styles.titleLight : {}, titleStyles]}>
          {title}
        </Text>
      </View>
    );
  }

  let subtitleElement = subtitle;
  if (typeof subtitle === 'string' || subtitle instanceof String) {
    subtitleElement = (
      <View style={styles.padding}>
        <Text allowFontScaling={allowFontScaling} style={[styles.subtitle, isLight ? styles.subtitleLight : {}, subTitleStyles]}>
          {t(subtitle)}
        </Text>
      </View>
    );
  }

  return (
    <View style={[styles.container, containerStyles, { width, height }]}>
      <View style={[styles.imageContainer, imageContainerStyles]}>{image}</View>
      {titleElement}
      {subtitleElement}
    </View>
  );
};

Page.propTypes = {
  isLight: PropTypes.bool.isRequired,
  image: PropTypes.element.isRequired,
  containerStyles: PropTypes.shape({
    style: PropTypes.any,
  }),
  imageContainerStyles: PropTypes.shape({
    style: PropTypes.any,
  }),
  title: PropTypes.oneOfType([PropTypes.string, PropTypes.element]).isRequired,
  subtitle: PropTypes.oneOfType([PropTypes.string, PropTypes.element])
    .isRequired,
  allowFontScaling: PropTypes.bool,
  titleStyles: PropTypes.shape({
    style: PropTypes.any,
  }),
  subTitleStyles: PropTypes.shape({
    style: PropTypes.any,
  }),
  width: PropTypes.number.isRequired,
  height: PropTypes.number.isRequired,
};

Page.defaultProps = {
  containerStyles: null,
  imageContainerStyles: null,
  allowFontScaling: true,
  titleStyles: null,
  subTitleStyles: null,
};

const { width, height } = Dimensions.get('window');
const potrait = height > width;

const styles = {
  container: {
    flex: 1,
    flexDirection: 'column',
    alignItems: 'center',
   // justifyContent: potrait ? 'center' : 'flex-start',
    paddingTop: potrait ? 0 : 10,
  },
  imageContainer: {
    flex: 0,
    paddingBottom: potrait ? 60 : 10,
    alignItems: 'center',
    width: '100%',
  },
  padding: {
    paddingHorizontal: 10,
  },
  title: {
    textAlign: 'center',
    fontSize: 26,
    color: '#fff',
    paddingBottom: 15,
  },
  titleLight: {
    color: '#000',
  },
  subtitle: {
    textAlign: 'center',
    fontSize: 16,
    color: 'rgba(255, 255, 255, 0.7)',
  },
  subtitleLight: {
    color: 'rgba(0, 0, 0, 0.7)',
  },
};

export default Page;

package.json file

"react": "18.3.1",
    "react-i18next": "^15.0.1",
    "react-native": "0.75.1",

How to resolve this issue.


Solution

  • The warning is self-descriptive. You're getting the warning because you used Page.defaultProps which would be removed in the future major version release. If you want to get rid of the warning, you can get rid of the Page.defaultProps and set default values for your props instead.

    import { Dimensions, Text, View } from 'react-native';
    import PropTypes from 'prop-types';
    import React from 'react';
    import { useTranslation } from 'react-i18next';
    
    const Page = ({
      isLight,
      image,
      title,
      subtitle,
      width,
      height,
      containerStyles = null,
      imageContainerStyles = null,
      allowFontScaling = true,
      titleStyles = null,
      subTitleStyles = null,
    }) => {
      let titleElement = title;
      const {t} = useTranslation()
      if (typeof title === 'string' || title instanceof String) {
        titleElement = (
          <View style={styles.padding}>
            <Text allowFontScaling={allowFontScaling} style={[styles.title, isLight ? styles.titleLight : {}, titleStyles]}>
              {title}
            </Text>
          </View>
        );
      }
    
      let subtitleElement = subtitle;
      if (typeof subtitle === 'string' || subtitle instanceof String) {
        subtitleElement = (
          <View style={styles.padding}>
            <Text allowFontScaling={allowFontScaling} style={[styles.subtitle, isLight ? styles.subtitleLight : {}, subTitleStyles]}>
              {t(subtitle)}
            </Text>
          </View>
        );
      }
    
      return (
        <View style={[styles.container, containerStyles, { width, height }]}>
          <View style={[styles.imageContainer, imageContainerStyles]}>{image}</View>
          {titleElement}
          {subtitleElement}
        </View>
      );
    };
    
    Page.propTypes = {
      isLight: PropTypes.bool.isRequired,
      image: PropTypes.element.isRequired,
      containerStyles: PropTypes.shape({
        style: PropTypes.any,
      }),
      imageContainerStyles: PropTypes.shape({
        style: PropTypes.any,
      }),
      title: PropTypes.oneOfType([PropTypes.string, PropTypes.element]).isRequired,
      subtitle: PropTypes.oneOfType([PropTypes.string, PropTypes.element])
        .isRequired,
      allowFontScaling: PropTypes.bool,
      titleStyles: PropTypes.shape({
        style: PropTypes.any,
      }),
      subTitleStyles: PropTypes.shape({
        style: PropTypes.any,
      }),
      width: PropTypes.number.isRequired,
      height: PropTypes.number.isRequired,
    };
    
    const { width, height } = Dimensions.get('window');
    const potrait = height > width;
    
    const styles = {
      container: {
        flex: 1,
        flexDirection: 'column',
        alignItems: 'center',
       // justifyContent: potrait ? 'center' : 'flex-start',
        paddingTop: potrait ? 0 : 10,
      },
      imageContainer: {
        flex: 0,
        paddingBottom: potrait ? 60 : 10,
        alignItems: 'center',
        width: '100%',
      },
      padding: {
        paddingHorizontal: 10,
      },
      title: {
        textAlign: 'center',
        fontSize: 26,
        color: '#fff',
        paddingBottom: 15,
      },
      titleLight: {
        color: '#000',
      },
      subtitle: {
        textAlign: 'center',
        fontSize: 16,
        color: 'rgba(255, 255, 255, 0.7)',
      },
      subtitleLight: {
        color: 'rgba(0, 0, 0, 0.7)',
      },
    };
    
    export default Page;