A decorator for defining a new component. Parameters: function: The component's :meth:`reactpy.core.proto.ComponentType.render` function.
(
function: Callable[..., ComponentType | VdomDict | str | None]
)
| 8 | |
| 9 | |
| 10 | def component( |
| 11 | function: Callable[..., ComponentType | VdomDict | str | None] |
| 12 | ) -> Callable[..., Component]: |
| 13 | """A decorator for defining a new component. |
| 14 | |
| 15 | Parameters: |
| 16 | function: The component's :meth:`reactpy.core.proto.ComponentType.render` function. |
| 17 | """ |
| 18 | sig = inspect.signature(function) |
| 19 | |
| 20 | if "key" in sig.parameters and sig.parameters["key"].kind in ( |
| 21 | inspect.Parameter.KEYWORD_ONLY, |
| 22 | inspect.Parameter.POSITIONAL_OR_KEYWORD, |
| 23 | ): |
| 24 | msg = f"Component render function {function} uses reserved parameter 'key'" |
| 25 | raise TypeError(msg) |
| 26 | |
| 27 | @wraps(function) |
| 28 | def constructor(*args: Any, key: Any | None = None, **kwargs: Any) -> Component: |
| 29 | return Component(function, key, args, kwargs, sig) |
| 30 | |
| 31 | return constructor |
| 32 | |
| 33 | |
| 34 | class Component: |