-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Implement on_mount
and on_unmount
for all components.
#1636
Conversation
These end up being a little weird due to react StrictMode when running the dev server... so the events are kind of duplicated, since the useEffect gets called twice essentially.
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.
Works great for me, some minor comments.
I think we should replace the on_load
handler with this once it's merged, and deprecate that way. One less thing to handle in the hydration. (Or are there any limitations to that?)
return ( | ||
EVENT_TRIGGERS | ||
| set(self.get_controlled_triggers()) | ||
| set((constants.ON_MOUNT, constants.ON_UNMOUNT)) |
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.
Shouldn't we just add these to the default EVENT_TRIGGERS
?
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.
i went back and forth on this, and ultimately did it this way because these aren't "real" event triggers that come from browser events, but are synthetic events that reflex handles.
@@ -182,6 +183,24 @@ def format_string(string: str) -> str: | |||
return string | |||
|
|||
|
|||
def format_var(var: Var) -> str: |
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.
Shouldn't this be the same as the Var.str method? Should we move this code there? This was the intention of that method.
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.
hmm, it's a little different. i cribbed this from Tag.format_prop
, so i assumed it must be doing something special. i'll see if i can combine them without breaking tests, so we can just use str(var)
instead.
[ | ||
"(() => {", | ||
format_var(event_chain), | ||
f"; preventDefault({format_var(event_arg)})" if event_arg else "", |
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.
The Event
function in state.js
should also handle the preventDefault
logic I thought?
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.
if we're formatting an arbitrary var as an event chain, there's no guarantee that Event
gets called at all; it could be anything.
We also need to update |
I meant to get to it today, but it got away from me. Definitely will have to land that before this can come in. |
This reverts commit 5959362.
Now the ref hooks are managed by a separate internal function so that downstream component wrappers can worry less about breaking framework functionality when overriding `_get_hooks`; but this means any internal functionality that was previously overriding `_get_hooks` was still unconditionally getting the base `_get_ref_hooks` functionality, that is was intentionally trying to override. Really glad we wrote integration tests for this behavior!
All components expose
on_mount
andon_unmount
triggersThese are tied directly into the react lifecycle hook
useEffect
. Theon_mount
is called as the main part of the effect, withon_unmount
chain being called as the cleanup function. The ordering of when these are called for different components is non-deterministic. In dev mode, these triggers will fire twice, because react calls alluseEffect
hooks twice in StrictMode.REF-231
Expose
format_var
andformat_event_chain
Previously, these were internal to
Tag.format_props
, but exposing them here allows them to be reused in other contexts.Partial fix for REF-209, but leaving open until the
Tag
code can be refactored to use the new methods.Split
_get_hooks
into_get_hooks_internal
Make it easier to downstream components to override
_get_hooks
without having to worry about screwing up Reflex's ref and on_mount handling behavior.