MyGit

0.11.0

yewstack/yew

版本发布时间: 2020-01-06 18:00:39

yewstack/yew最新发布版本:yew-v0.21.0(2023-09-29 19:14:55)

This release aims to lay the groundwork for Yew component libraries as well as clean up the API for the ever elusive 1.0 release 😜

Transition Guide

This release comes with a lot of breaking changes. We understand it's a hassle to update projects but the Yew team felt it was necessary to rip a few bandaids off now as we approach a 1.0 release in the (hopefully) near future. To ease the transition, here's a guide which outlines the main refactoring you will need to do for your project. (Note: all of the changes are reflected in the many example projects if you would like a proper reference example)

1. Callback syntax

This is the main painful breaking change. It applies to both element listeners as well as Component callback properties. A good rule of thumb is that your components will now have to retain a ComponentLink to create callbacks on demand or initialize callbacks in your component's create() method.

Before:

struct Model;

enum Msg {
    Click,
}

impl Component for Model {
    type Message = Msg;
    type Properties = ();

    fn create(_: Self::Properties, _: ComponentLink<Self>) -> Self {
        Model
    }

    fn update(&mut self, msg: Self::Message) -> ShouldRender {
        match msg {
            Msg::Click => true,
        }
    }

    fn view(&self) -> Html<Self> {
        // BEFORE: Callbacks were created implicitly from this closure syntax
        html! {
            <button onclick=|_| Msg::Click>{ "Click me!" }</button>
        }
    }
}

After:

struct Model {
  link: ComponentLink<Self>,
}

enum Msg {
    Click,
}

impl Component for Model {
    type Message = Msg;
    type Properties = ();

    fn create(_: Self::Properties, link: ComponentLink<Self>) -> Self {
        Model { link }
    }

    fn update(&mut self, msg: Self::Message) -> ShouldRender {
        match msg {
            Msg::Click => true,
        }
    }

    fn view(&self) -> Html {
        // AFTER: Callbacks need to be explicitly created now
        let onclick = self.link.callback(|_| Msg::Click);
        html! {
            <button onclick=onclick>{ "Click me!" }</button>
        }
    }
}

If a closure has a parameter you will now need to specify the parameter's type. A tip for finding the appropriate type is to search Yew's repo for the HTML attribute the closure is assigned to.

For example, onkeydown of <button>:

let onkeydown = self.link.callback(|e: KeyDownEvent| {
    // ...
});

and

html! {
    <button onkeydown=onkeydown type="button">
        { "button" }
    </button>
}

2. Method Renames

It should be safe to do a project-wide find/replace for the following:

These renames will probably require some more care:

3. Drop Generic Types for Html<Self> -> Html

:tada: We are pretty excited about this change! The generic type parameter was confusing and restrictive and is now a thing of the past!

Before:

impl Component for Model {
    // ...

    fn view(&self) -> Html<Self> {
        html! { /* ... */ }
    }
}

After:

impl Component for Model {
    // ...

    fn view(&self) -> Html {
        html! { /* ... */ }
    }
}

4. Properties must implement Clone

In yew v0.8 we removed the requirement that component properties implement Clone and in this release we are adding the requirement again. This change is needed to improve the ergonomics of nested components. The only time properties will be cloned is when a wrapper component re-renders nested children components.


Changelog

相关地址:原始地址 下载(tar) 下载(zip)

查看:2020-01-06发行的版本