react-native-copilot icon indicating copy to clipboard operation
react-native-copilot copied to clipboard

Autostart on mount

Open Zeeshan10277 opened this issue 2 years ago • 12 comments

Whenever i try to start the tour from useEffect, It does'nt work even if am passing setTimeout to it. Example code is given below:

const tour = useCopilot() useEffect(()=>{ setTimeout(()=>{ tour.start("repaircat"); }, 600) },[])

In the first screen I have started the tour by passing start() to the button click but in the second screen i want to initiate the tour again by calling start with the name of step through useEffect.

Is there any way to start the tour without clicking any button?

Zeeshan10277 avatar May 30 '23 16:05 Zeeshan10277

I had similar issue but was able to fix it, Here is my solution

const { currentStepNumber } = useCopilot()
  const isMounted = useIsMounted();
  
useEffect(() => {
    const timeout = setTimeout(() => {
      if (isMounted.current && currentStepNumber === 0) {
        start();
      }
    }, TIMEOUT);
    return () => clearTimeout(timeout);
  }, [currentStepNumber, isMounted,  start]);
  

you can search for IsMounted Hook, but I can help you with it if you are unable to find any.

lurdharry avatar May 30 '23 21:05 lurdharry

The fix @lurdharry mentions does not work here.

Any update would be greatly appreciated.

Saigronas avatar Jun 22 '23 13:06 Saigronas

@Saigronas try replacing the useEffect hook with useFocusEffect hook from react navigation.

It was working well when I used useEffect but it stopped working at some point may be due to metro bundler cache issue, but since I changed to useFocusEffect , it has been working perfectly even in production. And also, you don’t need the userIsMounted hook when using useFocusEffect.

lurdharry avatar Jun 22 '23 13:06 lurdharry

Even by using useFocusEffect, (which i already used for another reason), i cannot get the tour to start on its own or by navigating to the screen.

However, when pressing "ctrl+s" and thereby refreshing the app, the tour starts. This was also the case with using useEffect

Saigronas avatar Jun 22 '23 14:06 Saigronas

@Saigronas did you get any solution?

tanmoygo avatar Jul 05 '23 20:07 tanmoygo

any update on the issue?

shreya-kalra-vs avatar Jul 25 '23 14:07 shreya-kalra-vs

I encountered an issue where the code wasn't functioning within the useEffect or useFocusEffect hooks. However, I managed to identify the solution that works perfectly for my use case in the code. To implement this, make sure to place the following code snippet within the top-level container:

<View onLayout={() => start()}>

work4m avatar Aug 29 '23 09:08 work4m

@Saigronas did you get any solution?

NewDimix avatar Apr 18 '24 11:04 NewDimix

same problem here, the start() doesn't work if it is called inside of a useEffect

carl0shd avatar May 07 '24 17:05 carl0shd

Thanks for the inspiration @work4m. This worked like magic, it even worked with scrollview

  const debouncedStart = debounce(() => {
      start();
    }, 5000);
  
    const runTutorial=()=>{
      if(!homepageTutorialComplete && !startTutorial){
          // Call the debounced start function
          debouncedStart();
          }
    }
  <View onLayout={() => runTutorial()}> 

lutakyn avatar Jul 18 '24 01:07 lutakyn

<View onLayout={() => runTutorial()}> Yes it is worked like a magic!!

yesudoss-aeq avatar Jul 19 '24 08:07 yesudoss-aeq