This repository was archived by the owner on Jul 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 46
3/8 add trap events page #84
Open
rlasjunies
wants to merge
2
commits into
yewstack:master
Choose a base branch
from
rlasjunies:trap-events
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,311 @@ | ||
--- | ||
description: Component could mutate internal state or re-render based on events emitted by html or yew components | ||
--- | ||
|
||
# Trap events and mutate state | ||
rlasjunies marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
The framework provide the capability to update the internal state, for example, when an event is emitted by a child component. | ||
|
||
The `update` method could be called and mutate the internal state. The `update` method is called via `self.link.callback`, `link` being an attribute of the component struct. | ||
|
||
The `update` method receives "context" by the argument `msg` of type `Self::Message`. You can define any type for `Message`. The common way is to define an enum `Msg` for any action that can mutate the state. Then define `Msg` as the type of `Message` in the Component trait implementation. | ||
|
||
At the end of the `update` method, you can decide to render the component returning `true`. | ||
rlasjunies marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
```rust | ||
use yew::prelude::*; | ||
|
||
pub struct TrapEventComponent { | ||
link: ComponentLink<Self>, | ||
|
||
name: String, | ||
show_message: bool, | ||
} | ||
|
||
pub enum Msg { | ||
Click(), | ||
} | ||
|
||
impl Component for TrapEventComponent { | ||
type Message = Msg; | ||
type Properties = (); | ||
|
||
fn create(_props: Self::Properties, link: ComponentLink<Self>) -> Self { | ||
Self { | ||
link, | ||
name: "Clark".into(), | ||
show_message: false, | ||
} | ||
} | ||
|
||
fn update(&mut self, msg: Self::Message) -> ShouldRender { | ||
match msg { | ||
Msg::Click() => self.show_message = true, | ||
} | ||
true | ||
} | ||
|
||
fn change(&mut self, _props: Self::Properties) -> ShouldRender { | ||
true | ||
} | ||
|
||
fn view(&self) -> Html { | ||
if !self.show_message { | ||
html! { | ||
<> | ||
<button onclick=self.link.callback( |_| Msg::Click() )>{"Click here!"}</button> | ||
</> | ||
} | ||
} else { | ||
html! { | ||
<> | ||
<h1>{format!("Hello {}",self.name)}</h1> | ||
</> | ||
} | ||
} | ||
} | ||
} | ||
``` | ||
|
||
## Define the `link` attribute in the state | ||
|
||
```rust | ||
# use yew::prelude::*; | ||
// ... | ||
pub struct TrapEventComponent { | ||
link: ComponentLink<Self>, | ||
|
||
// ... | ||
# name: String, | ||
# show_message: bool, | ||
# } | ||
# | ||
# pub enum Msg { | ||
# Click(), | ||
rlasjunies marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# } | ||
# | ||
# impl Component for TrapEventComponent { | ||
# type Message = Msg; | ||
# type Properties = (); | ||
# | ||
# fn create(_props: Self::Properties, link: ComponentLink<Self>) -> Self { | ||
# Self { | ||
# link, | ||
# name: "Clark".into(), | ||
# show_message: false, | ||
# } | ||
# } | ||
# | ||
# fn update(&mut self, msg: Self::Message) -> ShouldRender { | ||
# match msg { | ||
# Msg::Click() => self.show_message = true, | ||
rlasjunies marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# } | ||
# true | ||
# } | ||
# | ||
# fn change(&mut self, _props: Self::Properties) -> ShouldRender { | ||
# true | ||
# } | ||
# | ||
# fn view(&self) -> Html { | ||
# if !self.show_message { | ||
# html! { | ||
# <> | ||
# <button onclick=self.link.callback( |_| Msg::Click() )>{"Click here!"}</button> | ||
rlasjunies marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# </> | ||
# } | ||
# } else { | ||
# html! { | ||
# <> | ||
# <h1>{format!("Hello {}",self.name)}</h1> | ||
rlasjunies marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# </> | ||
# } | ||
# } | ||
# } | ||
# } | ||
``` | ||
|
||
## Define a Message enum | ||
|
||
```rust | ||
# use yew::prelude::*; | ||
# | ||
# pub struct TrapEventComponent { | ||
# link: ComponentLink<Self>, | ||
# | ||
rlasjunies marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# name: String, | ||
# show_message: bool, | ||
# } | ||
|
||
// ... | ||
pub enum Msg { | ||
Click(), | ||
} | ||
|
||
impl Component for TrapEventComponent { | ||
type Message = Msg; | ||
type Properties = (); | ||
|
||
// ... | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This comment should be indented in line with the code after. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure. This is a "documentation information" it's not a code comment. I had to do in this way due to the automatic tests. Temporarly I remove the automatic tests. For more readability, I test the example in a dedicated project https://github.com/rlasjunies/yew-doc-examples I will add again the tests if it's really required. |
||
# fn create(_props: Self::Properties, link: ComponentLink<Self>) -> Self { | ||
# Self { | ||
# link, | ||
# name: "Clark".into(), | ||
# show_message: false, | ||
# } | ||
# } | ||
# | ||
# fn update(&mut self, msg: Self::Message) -> ShouldRender { | ||
# match msg { | ||
# Msg::Click() => self.show_message = true, | ||
rlasjunies marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# } | ||
# true | ||
# } | ||
# | ||
# fn change(&mut self, _props: Self::Properties) -> ShouldRender { | ||
# true | ||
# } | ||
# | ||
# fn view(&self) -> Html { | ||
# if !self.show_message { | ||
# html! { | ||
# <> | ||
# <button onclick=self.link.callback( |_| Msg::Click() )>{"Click here!"}</button> | ||
# </> | ||
# } | ||
# } else { | ||
# html! { | ||
# <> | ||
# <h1>{format!("Hello {}",self.name)}</h1> | ||
# </> | ||
# } | ||
# } | ||
# } | ||
# } | ||
|
||
|
||
|
||
rlasjunies marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
``` | ||
|
||
## Update the internal state based on the context | ||
|
||
```rust | ||
# use yew::prelude::*; | ||
# | ||
# pub struct TrapEventComponent { | ||
# link: ComponentLink<Self>, | ||
# | ||
# name: String, | ||
# show_message: bool, | ||
# } | ||
# | ||
# pub enum Msg { | ||
# Click(), | ||
rlasjunies marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# } | ||
# | ||
# impl Component for TrapEventComponent { | ||
# type Message = Msg; | ||
# type Properties = (); | ||
# | ||
# fn create(_props: Self::Properties, link: ComponentLink<Self>) -> Self { | ||
# Self { | ||
# link, | ||
# name: "Clark".into(), | ||
# show_message: false, | ||
# } | ||
# } | ||
|
||
// ... | ||
fn update(&mut self, msg: Self::Message) -> ShouldRender { | ||
match msg { | ||
Msg::Click() => self.show_message = true, | ||
} | ||
true | ||
} | ||
// ... | ||
|
||
# fn change(&mut self, _props: Self::Properties) -> ShouldRender { | ||
# true | ||
# } | ||
# | ||
# fn view(&self) -> Html { | ||
# if !self.show_message { | ||
# html! { | ||
# <> | ||
rlasjunies marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# <button onclick=self.link.callback( |_| Msg::Click() )>{"Click here!"}</button> | ||
# </> | ||
# } | ||
# } else { | ||
# html! { | ||
# <> | ||
rlasjunies marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# <h1>{format!("Hello {}",self.name)}</h1> | ||
# </> | ||
# } | ||
# } | ||
# } | ||
# } | ||
|
||
``` | ||
|
||
## Trap the html native events | ||
rlasjunies marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
```rust | ||
# use yew::prelude::*; | ||
# | ||
# pub struct TrapEventComponent { | ||
# link: ComponentLink<Self>, | ||
# | ||
# name: String, | ||
# show_message: bool, | ||
# } | ||
# | ||
# pub enum Msg { | ||
# Click(), | ||
# } | ||
# | ||
# impl Component for TrapEventComponent { | ||
# type Message = Msg; | ||
# type Properties = (); | ||
# | ||
# fn create(_props: Self::Properties, link: ComponentLink<Self>) -> Self { | ||
# Self { | ||
# link, | ||
# name: "Clark".into(), | ||
# show_message: false, | ||
# } | ||
# } | ||
# | ||
# fn update(&mut self, msg: Self::Message) -> ShouldRender { | ||
# match msg { | ||
# Msg::Click() => self.show_message = true, | ||
# } | ||
# true | ||
# } | ||
# | ||
# fn change(&mut self, _props: Self::Properties) -> ShouldRender { | ||
# true | ||
# } | ||
# | ||
# fn view(&self) -> Html { | ||
# if !self.show_message { | ||
// ... | ||
html! { | ||
<> | ||
rlasjunies marked this conversation as resolved.
Show resolved
Hide resolved
|
||
<button onclick=self.link.callback( |_| Msg::Click() )>{"Click here!"}</button> | ||
</> | ||
// ... | ||
# } | ||
# } else { | ||
# html! { | ||
# <> | ||
rlasjunies marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# <h1>{format!("Hello {}",self.name)}</h1> | ||
# </> | ||
# } | ||
# } | ||
# } | ||
# } | ||
``` | ||
|
||
**TODO: extend adding the treatment based on an event state or redirect to example** |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.