Why not have python setattr function instead of use_state #1102
Replies: 4 comments 7 replies
-
This reminds me of the signal pattern. |
Beta Was this translation helpful? Give feedback.
-
We have looked into this and it is technologically realistic. The reason we current don't currently do this was a design decision to have a 1:1 match with the ReactJS's API. If Facebook/Meta implements At least for now, if/when we implement this it will be a separate Python package. |
Beta Was this translation helpful? Give feedback.
-
[email protected] happens to be implemented like this 😎 |
Beta Was this translation helpful? Give feedback.
-
This python uses a generic version of reference variables, which is similar to Ref in Vue You can wear any type But it needs to be integrated into this framework, otherwise the value of the dom element cannot be changed from typing import Generic, TypeVar
T = TypeVar('T')
class Ref(Generic[T]):
def __init__(self, value: T = None):
self._value: T = value
@property
def current(self) -> T:
return self._value
@current.setter
def current(self, value: T):
self._value = value
num = Ref(1)
print(num.current) # 1
num.current = 2
print(num.current) # 2 |
Beta Was this translation helpful? Give feedback.
-
Starting this discussion which core developers may not like, as I know it involves a lot of work under the whole library again.
Here is how it goes:
React uses a hook called use_state to change dom when data changes and on the other side it changes the state (data) when the user makes any change to the UI.
We could try and use Python-like syntax of just updating the variable to a new value without calling the function set_whatever.
This seems possible with Python as Python has the method
__setattr__
which can handle all the changes and do pre-processing or post-processing needed.We could do something like:
FYI, I'm not familiar with React JS as much, so I cannot tell for sure if this is possible, but someone knowing React better can tell if this is feasible or not.
Beta Was this translation helpful? Give feedback.
All reactions