Component with Conditional Rendering - Yew Typing CST Test
Loading…
Component with Conditional Rendering — Yew Code
Shows different content based on a boolean state.
# yew/demo/conditional.rs
use yew::prelude::*;
struct Model {
is_logged_in: bool,
}
enum Msg {
Toggle,
}
impl Component for Model {
type Message = Msg;
type Properties = ();
fn create(ctx: &Context<Self>) -> Self {
Model { is_logged_in: false }
}
fn update(&mut self, ctx: &Context<Self>, msg: Self::Message) -> bool {
match msg {
Msg::Toggle => {
self.is_logged_in = !self.is_logged_in;
true
}
}
}
fn view(&self, ctx: &Context<Self>) -> Html {
html! {
<div>
{ if self.is_logged_in { html!{<p>{"Welcome back!"}</p>} } else { html!{<p>{"Please log in."}</p>} } }
<button onclick={ctx.link().callback(|_| Msg::Toggle)}>{"Toggle"}</button>
</div>
}
}
}
fn main() {
yew::start_app::<Model>();
}Yew Language Guide
Yew is a modern Rust framework for building client-side web applications using WebAssembly (Wasm), providing a reactive component-based architecture similar to React.
Primary Use Cases
- ▸Single-page applications (SPA) in Rust
- ▸Interactive dashboards and data visualization
- ▸WebAssembly-based web games
- ▸Frontend for Rust backend services
- ▸High-performance, low-latency web UIs
Notable Features
- ▸Component-based architecture with reusable components
- ▸Supports reactive updates via message passing
- ▸Virtual DOM for efficient rendering
- ▸WebAssembly backend for fast execution
- ▸Strong Rust type system ensures memory safety
Origin & Creator
Yew was created by Denis Kolodin in 2018 and has since evolved with community contributions into a robust Rust frontend framework.
Industrial Note
Yew is targeted at developers seeking high-performance web applications leveraging Rust's safety, concurrency, and memory efficiency while running in the browser.