简体   繁体   中英

Using require() to load functional component

My app takes a lot of time(15 secs 2GB RAM/10 secs 3GB RAM/ 5 secs 4GB RAM) for cold startup and I am trying to reduce this time. Inspired by Inline Requires and this I am trying to load some functional components on-demand. So I am doing this as -

DatePicker.tsx

import React from 'react';
import DateTimePicker, { DateTimePickerProps } from 'react-native-modal-datetime-picker';

...

export const DatePicker: React.FC<DatePickerProps> = (props) => {

return (
  <DateTimePicker
   date={props.date}
   isVisible={props.isDateTimePickerVisible}
   ...
  />
);

};

AddRecord.tsx

 //Intentionally commented import 
 //import { DatePicker } from '@aph/mobile-patients/src/components/ui/DatePicker';
 
 ...

 let DatePicker: any = null;

 export const AddRecord: React.FC<AddRecordProps> = (props) => {
    const [isDateTimePickerVisible, setIsDateTimePickerVisible] = useState<boolean>(false);

    return(
      <View>
        <Text  onPress={() => {
            setIsDateTimePickerVisible(true);

            if (DatePicker == null) {
              DatePicker = require('@aph/mobile-patients/src/components/ui/DatePicker');
            }

            Alert.alert('Clicked');

            console.log('check DatePicker ', DatePicker);
          }}> Click Me </Text>

          {isDateTimePickerVisible ? (
            <DatePicker
               isDateTimePickerVisible={isDateTimePickerVisible}
                ...
             />
          ) : null}
      </View>
    );
 }

But simulator is reporting an error在此处输入图像描述

Logging DatePicker object -

在此处输入图像描述

setIsDateTimePickerVisible is setting true before DatePicker has been loaded.

I am not sure but I guess it might be:

<Text  onPress={() => {
            if (DatePicker == null) {
              DatePicker = require('@aph/mobile-patients/src/components/ui/DatePicker');
            }

            Alert.alert('Clicked');
 
            console.log('check DatePicker ', DatePicker);
            setIsDateTimePickerVisible(true); // <- update the state after


          }}> Click Me </Text>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM