bug: React overlay hooks (useIon*) are not fully memorised
See original GitHub issuePrequisites
- I have read the Contributing Guidelines.
- I agree to follow the Code of Conduct.
- I have searched for existing issues that already report this problem, without success.
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
- Use any of the new overlay hooks e.g.
useIonToast
- Make it a dependency of
React.useEffect(..., [presentToast])
- 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:
- Created 2 years ago
- Comments:5 (4 by maintainers)
Top GitHub Comments
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.
Thanks for the PR! I am going to keep this issue open until the PR gets merged.