Can not send frontend request to fetch a string from the backend

Hi Rustgang!

I am currently working on a website with a frontend in yew and a backend in actix-web.

I want to fetch a part of the UI asynchronously, since I do not want to block the render to load that part.

Ultimately, I would like to use that to fetch files/images/objects from the backend. To get started, I have implement an MVP to fetch a string from an endpoint, but I am already having trouble.

I am using the code below:

use yew::prelude::*;
use wasm_bindgen_futures;
use gloo_net::http::Request;

#[function_component]
fn CollectionDisplay() -> Html {
    
    let collections = use_state(|| "failed".to_string());
    {
        let collections = collections.clone();
        use_effect_with_deps(move |_| {
            wasm_bindgen_futures::spawn_local(async move {
                let fetched_collections: String = Request::post("localhost:8081/get-user-collections")
                    .body("friedi")
                    .send()
                    .await
                    .unwrap()
                    .json()
                    .await
                    .unwrap();
                collections.set(fetched_collections);
            });
            || ()
        }, ());
    }
    html! {
        <div>
            {*collections}
        </div>
    }
}


#[function_component]
fn App() -> Html {
    html! {
        <div>
            <CollectionDisplay />
        </div>
    }
}

fn main() {
    yew::Renderer::<App>::new().render();
}

an I am getting the error "cannot move out of dereference of UseStateHandle<String>
move occurs because value has type String, which does not implement the Copy trait" on "*collections" in line 28. I was wondering if anyone could help me resolve this, or perhaps even tell me whether the approach is correct in the first place.

Very much appreciate any help

Could you try the following and see if it works?

1 Like

That did it actually, thank you so much!!!

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.