Build a sample app

Once you have the environment ready, you can either choose to use a starter template that contains the boilerplate needed for a basic Yew app or manually set up a small project.

Using a starter template

Install cargo-generate by following their installation instructions then run the following command:

  1. cargo generate --git https://github.com/yewstack/yew-trunk-minimal-template

Setting up the application manually

Create Project

To get started, create a new cargo project.

  1. cargo new yew-app

Open the newly created directory.

  1. cd yew-app

Run a hello world example

To verify the Rust environment is setup, run the initial project using cargo run. You should see a “Hello World!” message.

  1. cargo run
  2. # output: Hello World!

Setting up the project as a Yew web application

To convert this simple command line application to a basic Yew web application, a few changes are needed.

Update Cargo.toml

Add yew to the list of dependencies.

Cargo.toml

  1. [package]
  2. name = "yew-app"
  3. version = "0.1.0"
  4. edition = "2021"
  5. [dependencies]
  6. yew = { version = "0.20.0", features = ["csr"] }

Build a sample app - 图1info

You only need feature csr if you are building an application. It will enable the Renderer and all client-side rendering related code.

If you are making a library, do not enable this feature as it will pull in client-side rendering logic into the server-side rendering bundle.

If you need the Renderer for testing or examples, you should enable it in the dev-dependencies instead.

Update main.rs

We need to generate a template which sets up a root Component called App which renders a button that updates its value when clicked. Replace the contents of src/main.rs with the following code.

Build a sample app - 图2note

The call to yew::Renderer::<App>::new().render() inside the main function starts your application and mounts it to the page’s <body> tag. If you would like to start your application with any dynamic properties, you can instead use yew::Renderer::<App>::with_props(..).render().

main.rs

  1. use yew::prelude::*;
  2. #[function_component]
  3. fn App() -> Html {
  4. let counter = use_state(|| 0);
  5. let onclick = {
  6. let counter = counter.clone();
  7. move |_| {
  8. let value = *counter + 1;
  9. counter.set(value);
  10. }
  11. };
  12. html! {
  13. <div>
  14. <button {onclick}>{ "+1" }</button>
  15. <p>{ *counter }</p>
  16. </div>
  17. }
  18. }
  19. fn main() {
  20. yew::Renderer::<App>::new().render();
  21. }

Create index.html

Finally, add an index.html file in the root directory of your app.

index.html

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8" />
  5. <title>Yew App</title>
  6. </head>
  7. </html>

View your web application

Run the following command to build and serve the application locally.

  1. trunk serve

Trunk will rebuild your application if you modify any of its source code files.

Congratulations

You have now successfully setup your Yew development environment, and built your first web application.

Experiment with this application and review the examples to further your learning.