-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
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
[material-ui][useScrollTrigger] Do nothing if target is null #45441
base: master
Are you sure you want to change the base?
Conversation
87cb731
to
35cab5a
Compare
35cab5a
to
de8d14d
Compare
Netlify deploy previewhttps://deploy-preview-45441--material-ui.netlify.app/ Bundle size reportDetails of bundle changes (Toolpad) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@vipierozan99 Can you fix the failing CI? And in what case is window
not present?
2c7418b
to
c3e61da
Compare
c3e61da
to
680496f
Compare
whoops sorry, first time contributing here 😅, this should fix it. Thanks for the quick response! w.r.t. the window not being present this line seems to imply its possible, maybe in some testing environments?
My use case is different tho, I want the target to be an element that might not be mounted yet so I want the hook to do nothing in the meantime, does that make sense? |
Can you only call the hook when the target element exists? Instead of modifying Edit: I know hook shouldn't be called conditionally, but maybe you can avoid rendering that component where the hook is called. Basically in your parent component: if (!containerRef.current) return <ComponentWithoutHookInside />;
else return <ComponentWithHookInside /> |
well, yes, I could do that, but I don't think it's a nice DX, now the parent component has to know if a child is using that hook or not. I'm using it to animate a bunch of components on the page depending on the scroll of another element on the page. Right now I'm patching MUI using pnpm patch, but I thought its a nice change to contribute to MUI :) It's also not a breaking change because you have to explicitly pass |
setTrigger(false); | ||
return undefined; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would this work?
setTrigger(false); | |
return undefined; | |
return setTrigger(false); |
Currently if
options.target
isundefined
it tried to default to thewindow
.In my case I have another component that may or may not be mounted at a given time, so the ref to it might be null.
Right now I do:
but that will default to the
window
, and if its not present it will throw an error. Which is not what I want exactly.This change just disables the hook if
null
is explicitly passed.