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

Implement on_mount and on_unmount for all components. #1636

Merged
merged 12 commits into from
Aug 30, 2023
Merged

Conversation

masenf
Copy link
Collaborator

@masenf masenf commented Aug 20, 2023

All components expose on_mount and on_unmount triggers

These are tied directly into the react lifecycle hook useEffect. The on_mount is called as the main part of the effect, with on_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 all useEffect hooks twice in StrictMode.

REF-231

Expose format_var and format_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.

@masenf masenf added this to the v0.2.6 milestone Aug 21, 2023
Copy link
Contributor

@picklelo picklelo left a 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))
Copy link
Contributor

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 ?

Copy link
Collaborator Author

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:
Copy link
Contributor

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.

Copy link
Collaborator Author

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 "",
Copy link
Contributor

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?

Copy link
Collaborator Author

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.

@picklelo
Copy link
Contributor

We also need to update reflex-web to add docs for these new event triggers to pass the integration tests.

@masenf
Copy link
Collaborator Author

masenf commented Aug 23, 2023

We also need to update reflex-web to add docs for these new event triggers to pass the integration tests.

I meant to get to it today, but it got away from me. Definitely will have to land that before this can come in.

masenf added 4 commits August 25, 2023 11:39
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!
@picklelo picklelo merged commit 2392c52 into main Aug 30, 2023
@Alek99 Alek99 deleted the masenf/on_mount branch August 30, 2023 17:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants