[][src]Crate yew

Yew Framework - API Documentation

Yew is a modern Rust framework for creating multi-threaded front-end web apps with WebAssembly

  • Features a macro for declaring interactive HTML with Rust expressions. Developers who have experience using JSX in React should feel quite at home when using Yew.
  • Achieves high performance by minimizing DOM API calls for each page render and by making it easy to offload processing to background web workers.
  • Supports JavaScript interoperability, allowing developers to leverage NPM packages and integrate with existing JavaScript applications.

Supported Targets

  • wasm32-unknown-unknown
  • wasm32-unknown-emscripten - (feature = "std_web" is required)
  • asmjs-unknown-emscripten - (feature = "std_web" is required)

Important Notes

  • Yew is not (yet) production ready but is great for side projects and internal tools
  • Yew supports both web-sys and stdweb, developers must choose one or the other using the features "web_sys" and "std_web".
  • Building with cargo-web is not supported for web-sys
  • Docs.rs docs are built by default with the "web_sys" feature, for "std_web" docs, visit yew-stdweb

Example

use yew::prelude::*;

struct Model {
    link: ComponentLink<Self>,
    value: i64,
}

enum Msg {
    AddOne,
}

impl Component for Model {
    type Message = Msg;
    type Properties = ();
    fn create(_: Self::Properties, link: ComponentLink<Self>) -> Self {
        Self {
            link,
            value: 0,
        }
    }

    fn update(&mut self, msg: Self::Message) -> ShouldRender {
        match msg {
            Msg::AddOne => self.value += 1
        }
        true
    }

    fn view(&self) -> Html {
        html! {
            <div>
                <button onclick=self.link.callback(|_| Msg::AddOne)>{ "+1" }</button>
                <p>{ self.value }</p>
            </div>
        }
    }
}

fn main() {
    yew::initialize();
    App::<Model>::new().mount_to_body();
}

Re-exports

pub use self::prelude::*;

Modules

agent

This module contains types to support multi-threading in Yew.

app

This module contains the App struct, which is used to bootstrap a component in an isolated scope.

callback

This module contains structs to interact with Scopes.

components

This module contains useful components. At this moment it includes typed Select only.

events

The module that contains all events available in the framework.

format

Utility module to convert data to types and back by specific formats like: JSON, BSON, TOML, YAML, XML.

html

The main module which contents aliases to necessary items to create a template and implement update and view functions. Also this module contains declaration of Component trait which used to create own UI-components.

macros

This module contains macros which implements html! macro and JSX-like templates

prelude

The Yew Prelude

services

This module is a container of servies to interact with the external resources.

utils

This module contains useful utils to get information about the current document.

virtual_dom

This module contains the implementation of reactive virtual dom concept.

Macros

binary_format

This macro is used for a format that can be encoded as Binary. It is used in conjunction with a type definition for a tuple struct with one (publicly accessible) element of a generic type. Not all types that can be encoded as Binary can be encoded as Text. As such, this macro should be paired with the text_format macro where such an encoding works (e.g., JSON), or with the text_format_is_an_error macro for binary-only formats (e.g., MsgPack).

html

This macro implements JSX-like templates.

text_format

This macro is used for a format that can be encoded as Text. It is used in conjunction with a type definition for a tuple struct with one (publically accessible) element of a generic type. Since any type that can be encoded as Text can also be encoded as Binary, it should be used with the binary_format macro.

text_format_is_an_error

This macro is used for a format that can be encoded as Binary but can't be encoded as Text. It is used in conjunction with a type definition for a tuple struct with one (publically accessible) element of a generic type. This macro should be paired with the binary_format macro that defines the binary-only format.

Functions

initialize

Initializes yew framework. It should be called first.

run_loop

Starts event loop.

start_app

Starts an app mounted to a body of the document.

start_app_with_props

Starts an app mounted to a body of the document.