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: React overlay hooks (useIon*) are not fully memorised

See original GitHub issue

Prequisites

Ionic Framework Version

  • v4.x
  • v5.x
  • v6.x

Current Behavior

Using the new overlay hooks feels great and is a nice improvement in my opinion. When using them I noticed that for useIon{Toast|Alert|Popover|...} the results aren’t memorised and change each render. This will cause a great deal of overhead when the present/dismiss methods will be dependencies of other useEffects.

You can see a demo of this behaviour here

Expected Behavior

All the useIon* hooks should memorised the returned present/dismiss to match developers expectation.

Steps to Reproduce

  1. Use any of the new overlay hooks e.g. useIonToast
  2. Make it a dependency of React.useEffect(..., [presentToast])
  3. See that this effect is reevaluated each time the component is rendered.

Code Reproduction URL

https://codesandbox.io/s/ionic-hooks-memorisation-5o4yc?file=/src/App.tsx

Ionic Info

No response

Additional Information

I tracked this issue down to useController. Adding some React.useCallback here https://github.com/ionic-team/ionic-framework/blob/7315e0157b917d2e3586c64f8082026827812943/packages/react/src/hooks/useController.ts#L38 and here https://github.com/ionic-team/ionic-framework/blob/7315e0157b917d2e3586c64f8082026827812943/packages/react/src/hooks/useController.ts#L74 and a React.useMemo https://github.com/ionic-team/ionic-framework/blob/7315e0157b917d2e3586c64f8082026827812943/packages/react/src/hooks/useController.ts#L79-L82 will ensure that the controller is memorised. Looking at the other useIon* hooks should be straight forward. Fixing useIonToast would need a React.useCallback here https://github.com/ionic-team/ionic-framework/blob/7315e0157b917d2e3586c64f8082026827812943/packages/react/src/hooks/useIonToast.ts#L18

If you think this is something that should be implemented I can create a PR for these changes.

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
willmartiancommented, Oct 5, 2021

Thanks for the contribution on this issue @FWeinb! The fix for this issue has been merged and will be available in the next release of Ionic Framework.

1reaction
liamdebeasicommented, Aug 16, 2021

Thanks for the PR! I am going to keep this issue open until the PR gets merged.

Read more comments on GitHub >

github_iconTop Results From Across the Web

bug: React overlay hooks (useIon*) are not fully memorised ...
Expected Behavior. All the useIon* hooks should memorised the returned present / dismiss to match developers expectation. Steps to Reproduce.
Read more >
React Hooks Common Mistakes - Bugfender
In React Hooks, we don't need to memorize the entire react lifecycle and manage it in our code. In fact most of the...
Read more >
Introducing the New Overlay Hooks for Ionic React
In Ionic React 5.6, we packaged up a new set of hooks for controlling our overlay components that we think you might like....
Read more >
Rules of Hooks - React
Don't call Hooks inside loops, conditions, or nested functions. Instead, always use Hooks at the top level of your React function, before any...
Read more >
Overlay Component - React Data Grid
Overlay components allow you to add your own overlays to AG Grid. Use these when the provided overlays do not meet your requirements....
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