reflex icon indicating copy to clipboard operation
reflex copied to clipboard

typed mixins and ComponentState

Open benedikt-bartscher opened this issue 1 year ago β€’ 5 comments

benedikt-bartscher avatar Apr 30 '24 17:04 benedikt-bartscher

Tests are failing due to the reflex_chat example app which uses ComponentState without the new mixin __init_subclass__ kwarg.

benedikt-bartscher avatar Apr 30 '24 17:04 benedikt-bartscher

It seems like this change requires all ComponentState subclasses to now pass mixin=True in the class definition. Is that right? What is the advantage of that?

masenf avatar Apr 30 '24 17:04 masenf

@masenf yes it currently does as mentioned in my last comment. Maybe we can find a way to auto-set this for ComponentState.

The advantage is improved typing. Currently mixins do not inherit from rx.State directly which leads to a bad dev experience. Consider this example:

class Mixin:
  def reset_state(self):
    self.reset()

  def do_smth_with_router(self):
    print(self.router)

With this PR you can do this, which passes all the type checks:

class Mixin(rx.State, mixin=True):
  def reset_state(self):
    self.reset()

  def do_smth_with_router(self):
    print(self.router)

benedikt-bartscher avatar Apr 30 '24 17:04 benedikt-bartscher

Maybe we can find a way to auto-set this for ComponentState.

@masenf I just pushed a commit, which implements implicit mixin=True for ComponentState subclasses

benedikt-bartscher avatar Apr 30 '24 18:04 benedikt-bartscher

I just pushed one minor fix to make it work with generic typed state mixins

benedikt-bartscher avatar Apr 30 '24 23:04 benedikt-bartscher