reflex icon indicating copy to clipboard operation
reflex copied to clipboard

feat: Added two more options to setvar.

Open abulvenz opened this issue 1 year ago β€’ 6 comments

Either provide a dot-separated string to access fields on objects or use a var to describe which value to set.

This enables you to write something like this:

import reflex as rx

class Object(rx.Base):
    name: str = "1234"

class State(rx.State):
    message: str = "Default"
    object: Object = Object()

def index() -> rx.Component:
    return rx.container(
        rx.text(f"Object name: ", State.object.name),
        rx.input(value=State.object.name, on_change=State.setvar(State.object.name)),  # new syntax
        rx.input(value=State.object.name, on_change=State.setvar("object.name")),  #new syntax
        rx.text(f"Message: ", State.message),
        rx.input(value=State.message, on_change=State.setvar(State.message)),  # new syntax
        rx.input(value=State.message, on_change=State.setvar("message")),
    )

app = rx.App()
app.add_page(index)

It might need some polishing with regard to nonexisting fields on objects, which is not checked for now. But I want to share a first version to get feedback.

abulvenz avatar Jul 10 '24 20:07 abulvenz

use a var to describe which value to set

when i initially implemented setvar, this behavior was included, but a point was raised that it could introduce an incongruity and confusion.

consider the following code

rx.input(value=State.message, on_change=State.setvar(State.message))

it's not clear if i'm intending to set State.messages to the changed value, or if i'm trying to do indirection and set a var that is named the value of State.message to the changed value.

masenf avatar Jul 10 '24 20:07 masenf

I still like "the State.a.set thing" https://github.com/reflex-dev/reflex/pull/3163#issuecomment-2083439794
But, as @masenf pointed out, we need descriptor based vars and event handlers for this to pass type checkers.

benedikt-bartscher avatar Jul 10 '24 20:07 benedikt-bartscher

we need descriptor based vars and event handlers for this to pass type checkers

This is actually on the horizon. We're working on the Var refactor now and descriptor-based state Vars are part of that project.

masenf avatar Jul 10 '24 21:07 masenf

OK, I see. Sorry that I didn't do the history digging :+1:

Thanks!

abulvenz avatar Jul 10 '24 21:07 abulvenz

to be clear, i still think State.setvar("object.name") is sweet! we should keep that.

masenf avatar Jul 10 '24 21:07 masenf

to be clear, i still think State.setvar("object.name") is sweet! we should keep that.

Ah, sorry, tiny misunderstanding :smile: , so we are still in the game :game_die: . With your suggested change we can still write something like the following, which can help when refactoring, but typing is a bit :broken_heart:, but of course we can use a little # pyright: ignore[my-typing].

  rx.input(
      value=State.object.name, on_change=State.setvar(State.object.name._var_name)
  )

@benedikt-bartscher Do you think this could still help then with our wild internal meta-programming dreams?

Or to help the confused API-users, we rename the function from setvar to apply_event_to or apply_value_to or apply_to and read this: rx.input(value=State.message, on_click=State.apply_to(State.message))

abulvenz avatar Jul 11 '24 08:07 abulvenz

Marking this as draft until resynced with main. Some of the recent addition for typing event handler might unblock this PR.

Lendemor avatar Oct 25 '24 18:10 Lendemor

closing this due to inactivity, feels free to resurrect it when appropriate :+1:

adhami3310 avatar Jan 07 '25 23:01 adhami3310

closing this due to inactivity, feels free to resurrect it when appropriate πŸ‘

Thanks for closing it for me. FMPOV it lost its charm when making it string-based again which is unstable under refactoring and not the best option when working with IDEs.

abulvenz avatar Jan 21 '25 14:01 abulvenz