#Rendering a list in 0.5

1 messages · Page 1 of 1 (latest)

merry sparrow
#

Hi guys,

I'm trying to recreate what's documented in https://dioxuslabs.com/learn/0.5/reference/dynamic_rendering#rendering-lists

on a list that used use_ref() in the past but that now uses a signal.

But the part that comes directly from the documentation gives me this error:

error[E0597]: `search_results` does not live long enough
  --> front/src/page/create_bis.rs:58:24
   |
34 |     let mut search_results = use_signal(Vec::<ComputedResult>::new);
   |         ------------------ binding `search_results` declared here
...
58 |     let results_lock = search_results.read();
   |                        ^^^^^^^^^^^^^^-------
   |                        |
   |                        borrowed value does not live long enough
   |                        argument requires that `search_results` is borrowed for `'static`
...
91 | }
   | - `search_results` dropped here while still borrowed

error[E0597]: `results_lock` does not live long enough
  --> front/src/page/create_bis.rs:59:28
   |
58 |       let results_lock = search_results.read();
   |           ------------ binding `results_lock` declared here
59 |       let results_rendered = results_lock.iter().map(|r| {
   |                              ^^^^^^^^^^^^ borrowed value does not live long enough
60 | /         rsx! {
61 | |             li {
62 | |                 onclick: move |_| {
63 | |                     match r {
...  |
81 | |             }
82 | |         }
   | |_________- argument requires that `results_lock` is borrowed for `'static`
...
91 |   }
   |   - `results_lock` dropped here while still borrowed

What am I missing? I'm simply trying to avoid cloning the results that are in my signal. Thank you

#
#[derive(Clone, PartialEq)]
enum ComputedResult {
    Stop,
    Line,
    NotFound,
}

fn compute_results() -> Vec<ComputedResult> {
    vec![ComputedResult::Stop, ComputedResult::Line, ComputedResult::Stop]
}

#[component]
pub fn Search() -> Element {
    let mut search_results = use_signal(Vec::<ComputedResult>::new);
    let mut input_value = use_signal(|| "".to_string());

    let search_request = move |evt: FormEvent| {
        input_value.set(evt.value().clone());

        if !evt.value().is_empty() {
            spawn({
                async move {
                    match query_api_request::<Vec<SearchResult>>(vec![("q", &evt.value())], SEARCH_API_PATH).await {
                        Ok(new_results) => {
                            if new_results.is_empty() {
                                search_results.set(vec![ComputedResult::NotFound])
                            } else {
                                search_results.set(compute_results())
                            }
                        }
                        rest => panic!("temp error"),
                    }
                }
            });
        }
    };

    let results_lock = search_results.read();
    let results_rendered = results_lock.iter().map(|r| {
        rsx! {
            li {
                onclick: move |_| {
                    match r {
                        ComputedResult::NotFound => {
                            //do nothing
                        },
                        ComputedResult::Stop => {
                          input_value.set("stop_name".to_string());
                        },
                        ComputedResult::Line => {
                            input_value.set("line_name".to_string());
                        }
                    }
                },
                id: "0",
                tabindex: "0",
            }
        }
    });

    rsx! {
        p {
            {results_rendered}
        }
    }
}
fathom marlin
#

You will need to clone r before moving it into the onclick event handler

#

you can't do much about that

merry sparrow
#

This was indeed pretty straightforward, but good to know it is a necessity in this case. I suppose I got mislead by the error message. Thanks a lot for the quick response.