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