reactpy icon indicating copy to clipboard operation
reactpy copied to clipboard

Use props `dict` on components, rather than kwargs.

Open Archmonger opened this issue 2 years ago • 4 comments
trafficstars

Current Situation

There's been a lot of growing pains in the fact that components have a significantly different interface than reactpy.html elements.

Developing certain parts of reactpy core and/or reactpy related utilities has been significantly more difficult due to this interface mismatch.

Proposed Actions

The new component interface should match the VDOM interface, and will look like this:

# `key=...` should be declared within props, identical to the `reactpy.html` interface.
my_component({"prop_1": True, "key": 123}, child_1, child_2)

As a part of this change, we should rename VdomAttributes to Props.

Do we retain args/kwargs?

We can handle @component function definitions a few ways. Some of these ways feel more similar to ReactJS, but have problems with Python type hints.

  1. Force all users to only use kwargs
  2. Allow args/kwargs in function defs. Let Python unpacking operator handle the rest.
    • This would break type hints, since args can't be defined within a TypedDict
  3. Force all users to define a props: dict as their first parameter
    • However, forcing users to manually define a TypedDict for every component would be extremely inconvenient.

I'm personally leaning towards option 1.

Archmonger avatar May 24 '23 22:05 Archmonger

More precisely the interface should conform to this protocol.

rmorshea avatar May 24 '23 22:05 rmorshea

We can potentially retain type hints using this PEP https://peps.python.org/pep-0692/

Archmonger avatar Jun 07 '23 06:06 Archmonger

I spent more thought on this issue while rewriting the docs.

@rmorshea We need to decide what we will do with the user's component function defs. See my updates above.

Archmonger avatar Jun 08 '23 06:06 Archmonger

Note: we'll have to figure out how this will play work if the component function is a method.

rmorshea avatar Jun 09 '23 21:06 rmorshea