Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Should UseToastManager methods be safe to use in useEffect? #801

Closed
derekhawker opened this issue Sep 27, 2023 · 2 comments · Fixed by #802 or #803
Closed

Should UseToastManager methods be safe to use in useEffect? #801

derekhawker opened this issue Sep 27, 2023 · 2 comments · Fixed by #802 or #803
Labels
👷 refactor (improvement) Improvements in the code base

Comments

@derekhawker
Copy link

Hi,

I was trying to trigger toast methods inside a useEffect and got some eslint warnings because it wanted toast or notify to be added to the dependency array. But those references change anytime we create a new toast so adding to the dependency array triggers an infinite number of toasts to display.

Was a little surprised by this as I though they would behave more like useState setters. I was curious... do you think it would be more ergonomic if these hook methods had immutable references?

How to trigger:
Add the follow code snippet to useToastManager demo after handleClickOpenToast method

  const {notify} = toast;
  
  React.useEffect(()=>{
    handleClickOpenToast()
  },[notify])

image

@cheton cheton added the 👷 refactor (improvement) Improvements in the code base label Oct 1, 2023
@cheton
Copy link
Member

cheton commented Oct 1, 2023

The methods passed to the context lack the useCallback hook for function memoization, leading to unnecessary re-renders:

// https://github.com/trendmicro-frontend/tonic-ui/blob/master/packages/react/src/toast/ToastManager.js#L225-L234
const context = getMemoizedState({
    // Methods
    close,
    closeAll,
    find,
    findIndex,
    notify,
    update,

    // Properties
    placement: placementProp,

    // State
    state,
    setState,
});

I think this issue can be resolved by using the useCallback Hook for those methods in ToastManager. Moreover, I'll check whether it is necessary to update the useToastManager Hook.

@cheton
Copy link
Member

cheton commented Oct 13, 2023

@derekhawker

Could you help review the PR #802? It should solve the issue you pointed out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment