You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Currently ReactPy using a function called strictly_equals to determine if set_state is setting itself to a duplicate value.
This sometimes relies on python's is keyword to check identity.
Proposed Actions
Using is to check equality in Python doesn't exactly behave the same as the javascript equivalent (Object.is). The current set_state design is effectively if the old/new memory references are the same, which doesn't equate to whether their values are the same. This is honestly a bit annoying, and results in re-renders in scenarios were it was completely unneeded.
Python's __eq__ method is far more similar to JavaScript than Python's is. So realistically, we should be attempting to do an __eq__ check whenever possible.
I'd recommend trying to replicate this behavior instead of relying on == in Python because == can result in slow comparisons for large objects and because it can cause unexpected issues with __eq__ overloads (e.g. numpy and pandas will raise errors).
Perhaps there are some more edge cases that we can deal with in Python that could make this a bit more usable though.
In the interests of usability, I think it's better to take the O(n) performance hit. Python comparisons are definitely several orders of magnitude faster than websocket/http data transmission.
Currently, whenever I'm using ReactPy I'm constantly forced into writing my own __eq__ comparison functions prior to calling set_state, making this performance optimization a moot point.
Exceptions from numpy can be handled via with contextlib.suppress(Exception):. If an exception is generated, we will fallback to is comparison.
Activity
rmorshea commentedon Dec 15, 2024
IIRC React uses
===(isStrictlyEqual) to check for changes.I'd recommend trying to replicate this behavior instead of relying on
==in Python because==can result in slow comparisons for large objects and because it can cause unexpected issues with__eq__overloads (e.g. numpy and pandas will raise errors).Perhaps there are some more edge cases that we can deal with in Python that could make this a bit more usable though.
Archmonger commentedon Jan 26, 2025
In the interests of usability, I think it's better to take the
O(n)performance hit. Python comparisons are definitely several orders of magnitude faster than websocket/http data transmission.Currently, whenever I'm using ReactPy I'm constantly forced into writing my own
__eq__comparison functions prior to callingset_state, making this performance optimization a moot point.Exceptions from
numpycan be handled viawith contextlib.suppress(Exception):. If an exception is generated, we will fallback toiscomparison.