Why is the return value of useEffect not getting called when my component unmounts?

I have a screen when navigating to the next screen "unmount" i want to reset the state to be the initial values, but for some reason, it’s not triggered anymore

I use a separate useEffect just for cleanup, but I did not figure the issue!

code snippet

  const [selectedIndex, setSelectedIndex] = useState<number | null>(null);
  const [selectedItem, setSelectedItem] = useState<Service | undefined>(
    undefined,
  );
  const [isOpen, setIsOpen] = useState<boolean>(false);
  const [isClose, setIsClose] = useState<boolean>(false);


  React.useEffect(() => {
    getServices();
    let format = 'hh:mm:ss A';
    let timeNow = moment(time, format),
      startTime = moment('04:00:00 pm', format),
      EndTime = moment('11:00:00 pm', format);
    if (!timeNow.isBetween(startTime, EndTime)) {
      isOpen && Alert.alert('Please, Select Time between 04pm - 11pm');
      isOpen && setIsReady(false);
    } else {
      isOpen && setIsReady(true);
    }
  }, [time, getServices, isOpen]);

/**
      React.useEffect(
        () => () => {
          setIsOpen(false);
          setIsClose(false);
          setSelectedItem(undefined);
          setSelectedIndex(null);
          console.log('cleared'); // not logging
        },
        [],
      );
*/

// navigate to next screen 
const sendData = () => {
setIsClose(true);
setTimeout(() => {
  // setIsUnMount((prev) => !prev);
 // setIsOpen(false);
  setIsClose(false);
  setSelectedItem(undefined);
  setSelectedIndex(null);
  navigation.navigate('MapScreen', {
    serviceInfo: selectedItem,
    timeReport: moment(time, 'HH:mm:ss').format('hh:mm A'),
    dateReport: moment(date).format('YYYY-MM-DD'),
   });
  }, 100);
};



<BottomModal
        onDismissFC={() => setIsOpen(false)}
        openModal={isOpen}
        closeModal={isClose}
        points={[height * 0.66]}>
        <View style={{flex: 1, backgroundColor: Colors.background}}>
          .....

        </View>
      </BottomModal>

Modal component

import React, {useCallback, useMemo, useRef} from 'react';
import {StyleSheet, View} from 'react-native';
import {
  BottomSheetBackdrop,
  BottomSheetModal,
  BottomSheetModalProvider,
} from '@gorhom/bottom-sheet';
import {Colors} from '../styles';

interface Props {
  children: React.ReactNode;
  openModal?: boolean;
  closeModal?: boolean;
  points: number[] | string[];
  onDismissFC?: () => void;
}

const BottomModal = ({
  children,
  onDismissFC,
  points,
  openModal,
  closeModal,
}: Props) => {
  const bottomSheetModalRef = useRef<BottomSheetModal>(null);
  const snapPoints = useMemo(() => points, [points]);
  const handlePresentModalPress = useCallback(() => {
    bottomSheetModalRef.current?.present();
  }, []);

  const handleDismissPress = useCallback(() => {
    bottomSheetModalRef.current?.dismiss();
  }, []);

  React.useEffect(() => {
    openModal && handlePresentModalPress();
    closeModal && handleDismissPress();
  }, [openModal, closeModal, handlePresentModalPress, handleDismissPress]);
  return (
    <BottomSheetModalProvider>
      <BottomSheetModal
        backgroundComponent={() => <View style={styles.modalHandleStyle} />}
        ref={bottomSheetModalRef}
        backdropComponent={BottomSheetBackdrop}
        // animationDuration={250}
        onDismiss={onDismissFC}
        snapPoints={snapPoints}>
        <View style={styles.contentModalContainer}>{children}</View>
      </BottomSheetModal>
    </BottomSheetModalProvider>
  );
};

export default BottomModal;

const styles = StyleSheet.create({
  modalHandleStyle: {
    ...StyleSheet.absoluteFillObject,
    borderTopLeftRadius: 10,
    borderTopRightRadius: 10,
    backgroundColor: Colors.background,
  },
  contentModalContainer: {
    flex: 1,
    backgroundColor: Colors.background,
  },
});

232 thoughts on “Why is the return value of useEffect not getting called when my component unmounts?”

  1. Pingback: keto diet seizures
  2. I think this is one of the most significant information for me.
    And i’m glad reading your article. But want to remark on some general things, The web site
    style is ideal, the articles is really excellent : D. Good job,
    cheers

    Reply
  3. Hi, I do think this is an excellent blog. I stumbledupon it 😉 I am going to revisit once again since i
    have saved as a favorite it. Money and freedom is the
    best way to change, may you be rich and continue to guide others.

    Reply
  4. Undeniably consider that which you stated. Your favourite justification seemed to be on the internet the easiest factor to consider of.
    I say to you, I definitely get annoyed even as folks consider worries that they just do not understand about.
    You managed to hit the nail upon the highest and outlined out
    the entire thing without having side effect , other
    folks could take a signal. Will probably be back to get more.

    Thanks

    Reply
  5. Hi, I believe your blog might be having internet browser compatibility problems.
    Whenever I take a look at your blog in Safari, it looks fine however when opening in Internet Explorer, it’s got some overlapping issues.
    I merely wanted to provide you with a quick heads up!
    Besides that, fantastic blog!

    Reply
  6. What’s Happening i am new to this, I stumbled
    upon this I have discovered It positively useful and it has aided me out loads.
    I hope to give a contribution & assist other customers
    like its helped me. Great job.

    Reply
  7. Hey There. I discovered your blog the use of msn.
    This is an extremely smartly written article. I’ll be sure to bookmark it
    and return to read more of your useful information. Thank you
    for the post. I will certainly return.

    Reply
  8. Can I just say what a comfort to uncover an individual who genuinely understands what they are discussing online.
    You actually know how to bring an issue to light and make it important.
    A lot more people need to look at this and understand this side of
    your story. I was surprised you’re not more popular since you surely have the
    gift.

    Reply
  9. Woah! I’m really digging the template/theme of this site.
    It’s simple, yet effective. A lot of times
    it’s challenging to get that “perfect balance” between superb usability and appearance.
    I must say you’ve done a very good job with this. In addition, the blog loads
    extremely fast for me on Opera. Outstanding Blog!

    Reply
  10. Hi everyone, it’s my first pay a visit at this
    website, and article is actually fruitful in support of me, keep up posting these articles or reviews.

    My web site: 2022

    Reply
  11. I’ve been exploring for a little bit for any high-quality articles
    or blog posts on this kind of house . Exploring in Yahoo I
    finally stumbled upon this website. Studying this information So i’m happy to exhibit that I have an incredibly excellent uncanny feeling I found out just what I needed.
    I such a lot without a doubt will make sure to do not
    fail to remember this site and provides it a glance on a relentless basis.

    Reply

Leave a Comment