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-2887] [Unconfirmed] Updating a field in an object in a collection of rx.Base models does not send delta #3360

Open
masenf opened this issue May 21, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@masenf
Copy link
Collaborator

masenf commented May 21, 2024

Describe the bug
On a call, a user reported an error where updating a field in an object in a collection from a background task, did not reflect the changes in the frontend until the collection was reassigned self.files = self.files.

I tried to reproduce this issue with a minimal example and was not successful, it worked as expected for me. More investigation is needed.

To Reproduce

import asyncio

import reflex as rx


class User(rx.Base):
    name: str
    status: int


class State(rx.State):
    users: list[User] = [
        User(name="Alice", status=1),
        User(name="Bob", status=1),
        User(name="Charlie", status=1),
    ]

    @rx.background
    async def munge_user(self):
        for user in self.users:
            if user.status == 1:
                break
        async with self:
            user.status = 2
        await asyncio.sleep(5)
        async with self:
            user.status = 3


def index() -> rx.Component:
    return rx.container(
        rx.color_mode.button(position="top-right"),
        rx.vstack(
            rx.table.root(
                rx.table.header(
                    rx.table.row(
                        rx.table.column_header_cell("Name"),
                        rx.table.column_header_cell("Status"),
                    ),
                ),
                rx.table.body(
                    rx.foreach(
                        State.users,
                        lambda user: rx.table.row(
                            rx.table.cell(user.name),
                            rx.table.cell(
                                rx.cond(user.status == 2, rx.spinner(), rx.text(user.status)),
                            ),
                        ),
                    ),
                ),
            ),
            rx.button("Munge User", on_click=State.munge_user),
            spacing="5",
            justify="center",
            min_height="85vh",
        ),
        rx.logo(),
    )


app = rx.App()
app.add_page(index)

Expected behavior
Clicking the button should take the first user with status == 1, set it to 2, wait five seconds, then set it to 3. In the UI, if the user's status is 2, then it should show a loading spinner.

Specifics (please complete the following information):

  • Python Version: 3.11.8
  • Reflex Version: 0.5.1a3

REF-2887

@masenf masenf added the bug Something isn't working label May 21, 2024
@masenf masenf changed the title [Unconfirmed] Updating a field in an object in a collection of rx.Base models does not send delta [REF-2887] [Unconfirmed] Updating a field in an object in a collection of rx.Base models does not send delta May 21, 2024
@bigdelys
Copy link

I am seeing a similar issue with reflex 0.5.2: whether I use a list of strings or a list of dictionaries, when I change the state variable from my background task, the change does not show up when using forEach, like:

    rx.foreach(
        AppState.chapter_bodies,
        lambda chapter_body, i: chapter(
            f"Chapter {i + 1}",
            chapter_body,
            AppState.chapter_instructions[i],
            i,
        ),
    ),

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants