-
Notifications
You must be signed in to change notification settings - Fork 1.7k
docs: events, storage, and component field notes #6779
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -68,6 +68,48 @@ def index(): | |
| ) | ||
| ``` | ||
|
|
||
| ## Using with `rx.foreach` | ||
|
|
||
| To render a memoized component for each item of a list Var, wrap the call in a | ||
| lambda and pass the props by keyword. Also pass a `key` prop that uniquely | ||
| identifies each item — React uses it to track items across re-renders — but do | ||
| **not** declare `key` in the memo function's signature. It is forwarded | ||
| automatically and consumed by React. | ||
|
|
||
| ```python | ||
| from typing import TypedDict | ||
|
|
||
|
|
||
| class Task(TypedDict): | ||
| id: str | ||
| name: str | ||
|
|
||
|
|
||
| class TaskState(rx.State): | ||
| tasks: list[Task] = [ | ||
| {"id": "1", "name": "Write docs"}, | ||
| {"id": "2", "name": "Review PR"}, | ||
| ] | ||
|
|
||
|
|
||
| @rx.memo | ||
| def task_card(task: rx.Var[Task]) -> rx.Component: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When users copy this Useful? React with 👍 / 👎. |
||
| return rx.card(rx.text(task["name"])) | ||
|
|
||
|
|
||
| def index(): | ||
| return rx.vstack( | ||
| rx.foreach( | ||
| TaskState.tasks, | ||
| lambda task: task_card(task=task, key=task["id"]), | ||
| ), | ||
| ) | ||
| ``` | ||
|
Comment on lines
+74
to
+107
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The example passes The example should add |
||
|
|
||
| Inside the memo body, `task` is a `Var`, not a plain dict: index into it with | ||
| `task["name"]` or use it in f-strings, but do not iterate over it or call | ||
| Python dict methods like `.keys()` — only Var operations are available. | ||
|
|
||
| ## Forwarding Props with `rx.RestProp` | ||
|
|
||
| Use `rx.RestProp` to accept and forward arbitrary props (think `...rest` in JSX). Useful for thin wrappers that re-style a primitive without redeclaring every prop. | ||
|
|
||
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.
In the case where a textarea also needs custom key handling, this fallback tells users to implement the Enter-submit behavior with a normal Reflex
on_key_down, but the built-in implementation needs the raw JS event to calle.preventDefault()ande.target.form.requestSubmit()(packages/reflex-components-core/src/reflex_components_core/el/elements/forms.py:870-877), while the public key event only sends the key and modifier Vars to state handlers (packages/reflex-base/src/reflex_base/event/__init__.py:1028-1045). Following this advice still lets Enter insert a newline, and using.prevent_defaultwould suppress all key input rather than only Enter, so the docs should point to a custom JScustom_attrshandler or another supported pattern.Useful? React with 👍 / 👎.