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

[REF-3375] useMemo on generateUUID props to maintain consistent value #3708

Merged
merged 1 commit into from
Jul 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions reflex/vars.py
Original file line number Diff line number Diff line change
Expand Up @@ -2552,17 +2552,25 @@ def __call__(self, *args, **kwargs) -> BaseVar:


def get_uuid_string_var() -> Var:
"""Return a var that generates UUIDs via .web/utils/state.js.
"""Return a Var that generates a single memoized UUID via .web/utils/state.js.

useMemo with an empty dependency array ensures that the generated UUID is
consistent across re-renders of the component.

Returns:
the var to generate UUIDs at runtime.
A Var that generates a UUID at runtime.
"""
from reflex.utils.imports import ImportVar

unique_uuid_var_data = VarData(
imports={f"/{constants.Dirs.STATE_PATH}": {ImportVar(tag="generateUUID")}} # type: ignore
imports={
f"/{constants.Dirs.STATE_PATH}": {ImportVar(tag="generateUUID")}, # type: ignore
"react": "useMemo",
}
)

return BaseVar(
_var_name="generateUUID()", _var_type=str, _var_data=unique_uuid_var_data
_var_name="useMemo(generateUUID, [])",
_var_type=str,
_var_data=unique_uuid_var_data,
)
Loading