Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compile error using directives in SSR app #2448

Closed
lpotthast opened this issue Mar 19, 2024 · 1 comment
Closed

Compile error using directives in SSR app #2448

lpotthast opened this issue Mar 19, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@lpotthast
Copy link
Contributor

Describe the bug
Using a custom directive, like in

#[derive(Clone)]
struct MyStruct {}

fn mydirective(el: HtmlElement<AnyElement>, _v: MyStruct) {
    tracing::info!("ok");
    let _el = el.attr("data-foo", "foooo");
}

#[component]
pub fn Foo() -> impl IntoView {
    view! {
        <div
            use:mydirective=MyStruct {}
        >
            "foo"
        </div>
    }
}

results in an unexpected compile error on use:mydirective=MyStruct {} when building an SSR app

    Checking directives-ssr v0.1.0 (/home/lukas/dev/directives-ssr)
error[E0277]: expected a `Fn()` closure, found `MyStruct`
  --> src/app.rs:28:5
   |
28 | /     view! {
29 | |         <div
30 | |             use:mydirective=MyStruct {}
   | |                             ----------- this tail expression is of type `MyStruct`
31 | |         >
32 | |             "foo"
33 | |         </div>
34 | |     }
   | |_____^ expected an `Fn()` closure, found `MyStruct`
   |
   = help: the trait `std::ops::Fn<()>` is not implemented for `MyStruct`
   = note: wrap the `MyStruct` in a closure with no arguments: `|| { /* code */ }`
   = help: the following other types implement trait `IntoAttribute`:
             bool
             char
             isize
             i8
             i16
             i32
             i64
             i128
           and 26 others
   = note: required for `MyStruct` to implement `IntoAttribute`
   = note: this error originates in the macro `view` (in Nightly builds, run with -Z macro-backtrace for more info)
help: you might have meant to create the closure instead of a block
   |
28 |     |_| view! {
   |     +++

It seems that the view macro expects something IntoAttribute here, which it shouldn't.

Leptos Dependencies
The test code lives here:

https://github.com/lpotthast/test-leptos-directives-ssr

To Reproduce
Steps to reproduce the behavior:

  1. git clone https://github.com/lpotthast/test-leptos-directives-ssr.git
  2. cd test-leptos-directives-ssr
  3. cargo leptos watch or just a cargo check

Expected behavior
Expected that code to compile and execute the directive on the client (through the internal usage of create_effect).

Additional context
When the CSR feature is forced to be active using leptos = { version = "0.6.9", features = ["csr"] }, the app does compile.
And gracefully notifies about the incorrect selection of features...

Funny thing: This use of the directive wrapped in a custom component taking children always compiles, even without forcing the CSR feature...

#[derive(Clone)]
struct MyStruct {}

fn mydirective(el: HtmlElement<AnyElement>, _v: MyStruct) {
    tracing::info!("ok");
    let _el = el.attr("data-foo", "foooo");
}

#[component]
pub fn Foo() -> impl IntoView {
    view! {
        <Bar>
            <div
                use:mydirective=MyStruct {}
            >
                "foo"
            </div>
        </Bar>
    }
}

#[component]
pub fn Bar(children: Children) -> impl IntoView {
    view! {
        <div>
            { children() }
        </div>
    }
}
@gbj gbj added the bug Something isn't working label Mar 31, 2024
@gbj
Copy link
Collaborator

gbj commented Apr 1, 2024

Closed by #2477.

@gbj gbj closed this as completed Apr 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants