Ayano why are you starting another new series, Yew!!!
Because I want to~ Also you are right the series is called Yew.
Assuming the environment is set up based on the previous posts, we can now start to setup a sample project using Yew.
First, a rust project is needed.
1
cargo new source
and we need to add dependencies to the cargo.toml :
1 2 3 4 5 6 7
[package] name = "source" version = "0.1.0" edition = "2021" [dependencies] # this is the development version of Yew yew = { git = "https://github.com/yewstack/yew/", features = ["csr"] }
For our purpose, we will create a simple “hello world” webpage. In src/main.rs :
here the trunk is the dependencies we introduced earlier, mainly used for wasm
and we have our hello world~
Notice that if we inspect our code, we see:
Notice that in the body section there is exactly the same code we have in our app function in src/main.rs . Some of you might notice that “hmm, this is similar to jsx(that you define HTML with code and use that as a component)?”. Yeah you are correct. We can definately define use more complicated html code.
For example, this code in the yew example website:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
<h1>RustConf Explorer</h1> <div> <h3>Videos to watch</h3> <p>John Doe: Building and breaking things</p> <p>Jane Smith: The development process</p> <p>Matt Miller: The Web 7.0</p> <p>Tom Jerry: Mouseless development</p> </div> <div> <h3>John Doe: Building and breaking things</h3> <img src="https://placehold.co/640x360.png?text=Video+Player+Placeholder" thumbnail:https://github.com/kagurazaka-ayano/ImageSource/blob/215077e371c355fe3e2709f25b187b989c6a4844/00041-3060508023.png?raw=true /> </div>
can be transformed into this html! macro:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
html! { <> <h1>{ "RustConf Explorer" }</h1> <div> <h3>{"Videos to watch"}</h3> <p>{ "John Doe: Building and breaking things" }</p> <p>{ "Jane Smith: The development process" }</p> <p>{ "Matt Miller: The Web 7.0" }</p> <p>{ "Tom Jerry: Mouseless development" }</p> </div> <div> <h3>{ "John Doe: Building and breaking things" }</h3> thumbnail: https://github.com/kagurazaka-ayano/ImageSource/blob/215077e371c355fe3e2709f25b187b989c6a4844/00041-3060508023.png?raw=true </div> </> }
(There are 2 types of components, structual and functional, we will only cover functional components in this article and structural in later articles)
Similar to some UI frameworks, like WinUI or Qt, we can define components as the building block of the website. For example, we can extract the files in the App component to another component:
In this case, we defined a Properties to be the input of the FileList component. This component is responsible for converting a list of file to HTML object. Notice to be able to use this wrapper as a Property, we added a PartialEq
Suppose we want to show the detail of the file when we clicked it(as if we selected it). We can do this, similar to many game engine or UI framework, by binding a callback to its properties.
To do this, we will modify our component and the file wrapper, or component property to be precise(since its a property of the component). And we will make a new component to display the detail of the file.
The handle we defined are called hooks, and they are used to store states that can be accessed by other components with handlers. It’s like a singleton that records global states, but managed by yew instead of user(user can define hooks, too). I will probably cover them in later articles… probably.