-
-
Notifications
You must be signed in to change notification settings - Fork 324
Logging when set_state
is called and no changes occur
#565
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
Comments
React also has a @component
def SomeComponent():
is_online, set_is_online = use_state(False)
use_debug_value("is online" if is_online else "is offline")
... Which might log:
|
On a related note, does react have the same issues of mutability? |
Yes it does. There's a section in React's new documentation about it which I'm also writing for IDOM presently. |
React's doc suggest using |
While I could see this being a challenge in JavaScript, this could be resolved within the IDOM framework. From my perspective, I'm effectively calling Perhaps if I don't think there's a need to inherit the same limitations as react within the Python space. Thoughts? |
While it might feel like a limitation, this choice by React makes you think about mutations in the hope that it will encourage you to avoid them. While you can get away with mutating state and then forcing a render by setting a copy, it can introduce unexpected bugs. Consider that this: new_list = old_list + [value]
# or this even
new_list = old_list.copy()
new_list.append(value) Is subtlety different, from this: old_list.append(value)
new_list = old_list.copy() Because we've modified the Here's a somewhat more concrete example... Imagine that you have some message form with a dropdown for selecting a recipient and a button for sending messages. The catch is that sending the message takes some time, and we need to await some things before it actually gets sent. Consider the code below and try to spot the bug: @component
def MessageForm():
state, set_state = use_state({
"recipient": "Alice",
"message": ""
})
async def handle_send(event):
await some_long_task()
await send_message(state["recipient"], state["message"])
def handle_recipient_change(event):
state["recipient"] = event["target"]["value"]
new_state = state.copy()
set_state(new_state)
def handle_message_change(event):
state["message"] = event["target"]["value"]
new_state = state.copy()
set_state(new_state)
.... There's a very subtle bug here where, if the user attempts to send the message (which takes some time) and then immediately changes the recipient, the message will get sent to the recipient they just changed to rather than the recipient which was selected at the time they tried to send. A similar bug would happen with the |
closing. this is addressed by #568 and the |
Current Situation
Currently, there exists circumstances where
set_state
can be called and nothing will occur. For example, modifying a mutable class (such as adataclass
)Proposed Changes
Notify the user if
set_state
was called and no re-render was queued.Implementation Details
When in
IDOM_DEBUG_MODE
...Show either
logging.warning
orlogging.info
whenset_state
is called but a re-render was not trigged.The text was updated successfully, but these errors were encountered: