question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

bug: [ionic-react] React lifecycle hooks not rerun on array "deps" changes

See original GitHub issue

Bug Report

Ionic version:

[ ] 4.x [x] 5.x

Current behavior:

Following React LifeCycle Methods it seems we should use useIonViewWillEnter instead of useEffect in a React FC. In a IonPage i need to set some deps to useEffect in order to dispatch side effects (base on Redux state updates).

Passing the second argument (deps?: any[]) to any of the useIonView* hooks do not re-run the LifeCycleCallback function.

Expected behavior:

I’d expect the LifeCycleCallback to re-run when one of the dependencies value change in the FC. Using useEffect works as expected but (ref the blog post) Ionic will take the IonPage in the background so the useEffect callback will be executed even after the user navigates to other views because - for React - the component is still mounted. This would create unexpected side effects between different IonPage that are using Redux useSelector to trigger its own useEffect callbacks.

Steps to reproduce:

Related code: Sample stackblitz - see the console logs for useIonViewWillEnter vs useEffect

https://stackblitz.com/edit/ionic-react-loading-96xecj?file=LoadingPage.jsx

  const [value, setValue] = useState('');

  useIonViewWillEnter(() => {
    console.log('useIonViewWillEnter: value is:', value)
  }, [value])

  useEffect(() => {
    console.log('useEffect: value is:', value)
  }, [value])

Other information:

I’m not sure if this will be considered as a bug or not but - from the @ionic-react types i saw the deps array so i think there should be a way to force a re-run of these hooks.

import { LifeCycleCallback } from '../contexts/IonLifeCycleContext';
export declare const useIonViewWillEnter: (callback: LifeCycleCallback, deps?: any[]) => void;
export declare const useIonViewDidEnter: (callback: LifeCycleCallback, deps?: any[]) => void;
export declare const useIonViewWillLeave: (callback: LifeCycleCallback, deps?: any[]) => void;
export declare const useIonViewDidLeave: (callback: LifeCycleCallback, deps?: any[]) => void;

Thanks!

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
edolixcommented, Jul 29, 2020

Sorry the delay @liamdebeasi - i’ll try to provide you a new StackBlitz with the issue. Thanks again for now 👍

0reactions
ionitron-bot[bot]commented, Aug 28, 2020

Thanks for the issue! This issue is being locked to prevent comments that are not relevant to the original issue. If this is still an issue with the latest version of Ionic, please create a new issue and ensure the template is fully filled out.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using the Effect Hook - React
The Effect Hook lets you perform side effects in function components: ... class lifecycle methods, you can think of useEffect Hook as componentDidMount ......
Read more >
an effect doesn't run again when its dependencies change
The truth is that changing a value in the dependency list is not enough for React to re-run the effect! How come?. Let's...
Read more >
Hooks and state 102: the Dependency array in useEffect()
Only if one of the dependencies has changed since the last render, will the effect be rerun. If you specify an empty array,...
Read more >
Using React Hooks in Ionic React - Ionic Blog
Hooks completely change what functional components can do in React, bringing state, async operations such as fetch, and APIs like Context to ...
Read more >
Understanding common frustrations with React Hooks
Editor's note · “React Hook cannot be called inside a callback” · “React Hooks must be called in a React function component or...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found