diff --git a/website/docs/tutorial/index.mdx b/website/docs/tutorial/index.mdx index 5cefaf1b9d1..30887995b09 100644 --- a/website/docs/tutorial/index.mdx +++ b/website/docs/tutorial/index.mdx @@ -459,8 +459,6 @@ videos list from an external source. For this we will need to add the following For making the fetch call. - [`serde`](https://serde.rs) with derive features For de-serializing the JSON response -- [`wasm-bindgen-futures`](https://crates.io/crates/wasm-bindgen-futures) - For executing Rust Future as a Promise Let's update the dependencies in `Cargo.toml` file: @@ -470,7 +468,6 @@ Let's update the dependencies in `Cargo.toml` file: +yew = { git = "https://github.com/yewstack/yew/", features = ["csr", "serde"] } +gloo-net = "0.6" +serde = { version = "1.0", features = ["derive"] } -+wasm-bindgen-futures = "0.4" ``` Yew's `serde` feature enables integration with the `serde` crate, the important point for us is that @@ -481,12 +478,16 @@ When choosing dependencies make sure they are `wasm32` compatible! Otherwise you won't be able to run your application. ::: -Update the `Video` struct to derive the `Deserialize` trait: +Update the imports: -```rust {2,4-5} +```rust {2} use yew::prelude::*; +use serde::Deserialize; -// ... +``` + +Update the `Video` struct to derive the `Deserialize` trait: + +```rust {2} -#[derive(Clone, PartialEq)] +#[derive(Clone, PartialEq, Deserialize)] struct Video { @@ -497,12 +498,71 @@ struct Video { } ``` -Now as the last step, we need to update our `App` component to make the fetch request instead of using hardcoded data +Now we need to update our `App` component to fetch data. The modern yew way to do this is with [`use_future`](https://docs.rs/yew/0.23.0/yew/suspense/fn.use_future.html) and [``](https://yew.rs/docs/concepts/suspense). + +Alternatively, you can use [`yew::platform::spawn_local`](https://docs.rs/yew/latest/yew/platform/fn.spawn_local.html) +if hooks are unavailable, such as within struct components or standard functions. + +`use_future` suspends the component until the async operation completes, and `` shows a +fallback UI (e.g. a loading indicator) in the meantime. -```rust {2,6-50,59-60} +Update the imports: + +```rust {3-4} use yew::prelude::*; +use serde::Deserialize; +use gloo_net::http::Request; ++use yew::suspense::use_future; +``` +We split the data fetching logic into a child component (`VideosFetcher`) that returns `HtmlResult`, +while the parent `App` wraps it in ``: + +```rust {2-6,7-39} +use yew::suspense::use_future; ++#[derive(Properties, PartialEq)] ++struct VideosFetchProps { ++ on_click: Callback