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
2/8 add internal state page #83
Open
rlasjunies
wants to merge
3
commits into
yewstack:master
Choose a base branch
from
rlasjunies:internal-state
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 2 commits
Commits
Show all changes
3 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
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,169 @@ | ||
--- | ||
description: Component can maintain it's own state and render information depending on it | ||
--- | ||
|
||
# Internal State | ||
rlasjunies marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
The component can manage it's own state using the Rust struct that implement the trait `Component`. The HTML rendering is based on this state. | ||
rlasjunies marked this conversation as resolved.
Show resolved
Hide resolved
|
||
When state change there is possibility to re-render the component. | ||
rlasjunies marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
> TODO documentation on state mutation | ||
|
||
```rust | ||
use yew::prelude::*; | ||
|
||
pub struct InternalStateComponent { | ||
name:String, | ||
} | ||
|
||
impl Component for InternalStateComponent { | ||
type Message = (); | ||
type Properties = (); | ||
|
||
fn create(_props: Self::Properties, _link: ComponentLink<Self>) -> Self { | ||
Self { | ||
name: "Clark".into(), | ||
} | ||
} | ||
|
||
fn update(&mut self, _msg: Self::Message) -> ShouldRender { | ||
true | ||
} | ||
|
||
fn change(&mut self, _props: Self::Properties) -> ShouldRender { | ||
true | ||
} | ||
|
||
fn view(&self) -> Html { | ||
html! { | ||
<> | ||
<h1>{format!("Hello {}",self.name)}</h1> | ||
</> | ||
} | ||
} | ||
} | ||
``` | ||
|
||
## State definition | ||
rlasjunies marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Here we add the `name` field in the struct | ||
rlasjunies marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
```rust | ||
# use yew::prelude::*; | ||
# | ||
// ... | ||
pub struct InternalStateComponent { | ||
name:String, | ||
} | ||
// ... | ||
# | ||
# impl Component for InternalStateComponent { | ||
# type Message = (); | ||
# type Properties = (); | ||
# | ||
# fn create(_props: Self::Properties, _link: ComponentLink<Self>) -> Self { | ||
# Self { | ||
# name: "Clark".into(), | ||
# } | ||
# } | ||
# | ||
# fn update(&mut self, _msg: Self::Message) -> ShouldRender { | ||
# true | ||
rlasjunies marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# } | ||
# | ||
# fn change(&mut self, _props: Self::Properties) -> ShouldRender { | ||
# true | ||
rlasjunies marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# } | ||
# | ||
# fn view(&self) -> Html { | ||
# html! { | ||
# <> | ||
# <h1>{format!("Hello {}",self.name)}</h1> | ||
# </> | ||
# } | ||
# } | ||
# } | ||
|
||
``` | ||
|
||
## State initialization | ||
|
||
The component lifecycle will initialize the state in the `create` method. | ||
|
||
```rust | ||
# use yew::prelude::*; | ||
# | ||
# pub struct InternalStateComponent { | ||
# name:String, | ||
# } | ||
# | ||
# impl Component for InternalStateComponent { | ||
# type Message = (); | ||
# type Properties = (); | ||
// ... | ||
fn create(_props: Self::Properties, _link: ComponentLink<Self>) -> Self { | ||
Self { | ||
name: "Clark".into(), | ||
} | ||
} | ||
// ... | ||
# fn update(&mut self, _msg: Self::Message) -> ShouldRender { | ||
# true | ||
# } | ||
# | ||
# fn change(&mut self, _props: Self::Properties) -> ShouldRender { | ||
# true | ||
# } | ||
# | ||
# fn view(&self) -> Html { | ||
# html! { | ||
# <> | ||
# <h1>{format!("Hello {}",self.name)}</h1> | ||
# </> | ||
# } | ||
# } | ||
# } | ||
``` | ||
|
||
## Rendering based on the state value | ||
|
||
Using the `html!` macro we can render html based on the component state in the `view` method | ||
rlasjunies marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
> please refer to the `html!` macro documentation page for more detail on the HTML rendering | ||
rlasjunies marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
```rust | ||
# use yew::prelude::*; | ||
# | ||
# pub struct InternalStateComponent { | ||
# name:String, | ||
# } | ||
# | ||
# impl Component for InternalStateComponent { | ||
# type Message = (); | ||
# type Properties = (); | ||
# | ||
# fn create(_props: Self::Properties, _link: ComponentLink<Self>) -> Self { | ||
# Self { | ||
# name: "Clark".into(), | ||
# } | ||
# } | ||
# | ||
# fn update(&mut self, _msg: Self::Message) -> ShouldRender { | ||
# true | ||
# } | ||
# | ||
# fn change(&mut self, _props: Self::Properties) -> ShouldRender { | ||
# true | ||
# } | ||
# | ||
// ... | ||
fn view(&self) -> Html { | ||
html! { | ||
<> | ||
<h1>{format!("Hello {}",self.name)}</h1> | ||
</> | ||
} | ||
} | ||
// ... | ||
# } | ||
``` |
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.