Skip to content
This repository has been archived by the owner on Jul 19, 2020. It is now read-only.

2/8 add internal state page #83

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
* [Literals & Expressions](concepts/html/literals-and-expressions.md)
* [Components](concepts/html/components.md)
* [Components](concepts/components/README.md)
* [Internal state](concepts/components/internalstate.md)
* [Properties](concepts/components/properties.md)
* [Callbacks](concepts/components/callbacks.md)
* [Refs](concepts/components/refs.md)
Expand Down
169 changes: 169 additions & 0 deletions src/concepts/components/internalstate.md
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
rlasjunies marked this conversation as resolved.
Show resolved Hide resolved
---

# 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>
</>
}
}
// ...
# }
```