I am new to Rust and Yew and I need some assistance in applying a useState hook inside an app component in Yew that looks like this:
struct BookList
use gloo_net::http::Request;
use yew::prelude::*;
#[function_component(App)]
pub fn app() -> Html {
let (books: UseStateHandle<Vec<BookList>> = use_state(|| vec![]);
async fn fetch_books() {
let response: Vec<> = Request::get("http://localhost:3000/books");
let cloned_books = books.clone():
cloned_books.set(response.data);
}
html! {
<div>
{"App"}
</div>
}
}
I got the error mismatched types. Where I have the parens, it says it expected a struct UseStateHandle
, found tuple.
I'm not sure why you need an open bracket before books
but I assume that this is where the confusion with the tuple comes from.
let books = use_state(|| vec![]);
If you assign the type when you fetch the data I am speculating that Rust should be smart enough to understand from the later context that this should be a handle to a vector of BookList
. Thus, I would assign the type there (which you already almost had but it was missing the type):
let response: Vec<BookList> = Request::get("http://localhost:3000/books");
Note that I have not tried these. Feel free to comment with follow-up exceptions so we can together elaborate.