Skip to content

Use props dict on components, rather than kwargs. #993

Closed as not planned
Closed as not planned

Description

@Archmonger

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.

Activity

  1. rmorshea commented on May 24, 2023

    @rmorshea
    Collaborator

    More precisely the interface should conform to this protocol.

  2. Archmonger commented on Jun 7, 2023

    @Archmonger
    ContributorAuthor

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

  3. added this to the 2.0.0 milestone on Jun 8, 2023
  4. Archmonger commented on Jun 8, 2023

    @Archmonger
    ContributorAuthor

    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.

  5. rmorshea commented on Jun 9, 2023

    @rmorshea
    Collaborator

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

  6. removed this from the 2.0.0 milestone on Feb 2, 2025
  7. Archmonger commented on Feb 12, 2025

    @Archmonger
    ContributorAuthor

    I did some prototyping, and learned it's currently not possible to use ParamSpec to provide type hints to a dictionary.

    Given that components are functions, it feels natural for users to call them via the standard "python function interface" (AKA arguments within parenthesis)

    Additionally, changing the reactpy.html interface to kwargs makes the HTML "read wrong", where all HTML properties are now declared at the bottom of the elements, making it almost unreadable to figure out what prop belongs to which element in deeply nested structures.

    Unless the Python type hinting space makes some major changes, I think we're forced to continue down the road we've already paved. I've already raised the issue that the Python type hinting system has no way to "mutate" kwargs, so hopefully that gains some interest/traction.

  8. rmorshea commented on Feb 17, 2025

    @rmorshea
    Collaborator

    A draft of the template strings PEP is currently in review so optimizing the interface may not be necessary anyways if that lands.

  9. Archmonger commented on Feb 17, 2025

    @Archmonger
    ContributorAuthor

    Agreed, I recently made that same conclusion while talking about the reactpy.html discussion on Discord.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    priority-2-moderateShould be resolved on a reasonable timeline.release-majorWarrents a major releasetype-revisionAbout a change in functionality or behavior

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions