DioxusLabs / DioxusLabs/dioxus

Component props (fn params) should not be named `prop`

Open
#3,543 2 comments 0 reactions 0 assignees View on GitHub
documentation
Dominant language
Rust
Stars
39.1k
Forks
1.9k
Avg merge
4d 10h
Merged PRs (30d)
4

Description

**Problem**

The [Dioxus 0.6 component props doc](https://dioxuslabs.com/learn/0.6/reference/component_props/) suggests that it is okay to name a prop `prop`, e.g.
```rust
fn Likes(props: LikesProps) -> Element {...}
```
However, this (sometimes?) fails to compile. See below for the error message.

**Steps To Reproduce**

Simplified [shorthand example](https://github.com/DioxusLabs/dioxus/blob/main/examples/shorthand.rs).

Text search and replace (over complete file) from `props` to `a_props` does fix the problem.

```rust
//! Dioxus supports shorthand syntax for creating elements and components.

use dioxus::prelude::*;

fn main() {
dioxus::launch(app);
}

fn app() -> Element {
let props = 123;
let class = "class";
let id = "id";

// todo: i'd like it for children on elements to be inferred as the children of the element
// also should shorthands understand references/dereferences?
// ie **a, *a, &a, &mut a, etc
let children = rsx! { "Child" };

rsx! {
div { class, id, {&children} }
Component { props }
}
}

#[component]
fn Component(props: i32) -> Element {
rsx! {
div { "{props}" }
}
}
```

Resulting error message:
```text
error[E0277]: `Props` is not implemented for `i32`
--> web/src/shorthand.rs:19:5
|
19 | / rsx! {
20 | | div { class, id, {&children} }
21 | | Component { props }
22 | | }
| |_____^ Props
|
= help: the trait `dioxus::prelude::Properties` is not implemented for `i32`
= note: Props is a trait that is automatically implemented for all structs that can be used as props for a component
= note: If you manually created a new properties struct, you may have forgotten to add `#[derive(Props, PartialEq, Clone)]` to your struct
= help: the following other types implement trait `dioxus::prelude::Properties`:
()
dioxus::dioxus_core::error_boundary::ErrorBoundaryProps
dioxus::dioxus_core::fragment::FragmentProps
dioxus::dioxus_core::suspense::component::SuspenseBoundaryPropsWithOwner
dioxus::dioxus_document::LinkProps
dioxus::dioxus_document::MetaProps
dioxus::dioxus_document::ScriptProps
dioxus::dioxus_document::StyleProps
and 14 others
note: required by a bound in `dioxus::prelude::fc_to_builder`
--> /home/flo/.cargo/registry/src/index.crates.io-6f17d22bba15001f/dioxus-core-0.6.1/src/properties.rs:112:8
|
110 | pub fn fc_to_builder(_: impl ComponentFunction) ->

::Builder
| ------------- required by a bound in this function
111 | where
112 | P: Properties,
| ^^^^^^^^^^ required by this bound in `fc_to_builder`
= note: this error originates in the macro `rsx` (in Nightly builds, run with -Z macro-backtrace for more info)
```

**Expected behavior**

One of:
- Documentation clearly says this is not allowed and examples don't use it, and the `#[component]` macro detects this issue and reports it.
- It just compiles.

**Environment:**
- Dioxus version: v0.6.1
- Rust version: 1.83.0
- OS info: WSL, Ubuntu 22.04
- App platform: `web`

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.