Skip to content

Commit 72ff904

Browse files
Tidy up components docs (#1596)
* Tidy up components docs. * Apply suggestions from code review Co-authored-by: Simon <simon@siku2.io>
1 parent 5217ed9 commit 72ff904

1 file changed

Lines changed: 39 additions & 12 deletions

File tree

docs/concepts/components.md

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ description: Components and their lifecycle hooks
44
---
55
## What are Components?
66

7-
Components are the building blocks of Yew. They manage their own state and can render themselves to the DOM. Components are created by implementing the `Component` trait which describes the lifecycle of a component.
7+
Components are the building blocks of Yew. They manage their own state and can render themselves to the DOM. Components are created by implementing the `Component` trait for a type. The `Component`
8+
trait has a number of methods which need to be implemented; Yew will call these at different stages
9+
in the lifecycle of a component.
810

911
## Lifecycle
1012

@@ -18,7 +20,8 @@ Components are the building blocks of Yew. They manage their own state and can r
1820

1921
When a component is created, it receives properties from its parent component as well as a `ComponentLink`. The properties can be used to initialize the component's state and the "link" can be used to register callbacks or send messages to the component.
2022

21-
It is common to store the props and the link in your component struct, like so:
23+
It is common to store the props (data which can be passed from parent to child components) and the
24+
`ComponentLink` in your component struct, like so:
2225

2326
```rust
2427
pub struct MyComponent {
@@ -40,7 +43,11 @@ impl Component for MyComponent {
4043

4144
### View
4245

43-
Components declare their layout in the `view()` method. Yew provides the `html!` macro for declaring HTML and SVG nodes and their listeners as well as child components. The macro acts a lot like React's JSX, but uses Rust expressions instead of JavaScript.
46+
The `view` method allows you to describe how a component should be rendered to the DOM. Writing
47+
HTML-like code using Rust functions can become quite messy, so Yew provides a macro called `html!`
48+
for declaring HTML and SVG nodes (as well as attaching attributes and event listeners to them) and a
49+
convenient way to render child components. The macro is somewhat similar to React's JSX (the
50+
differences in programming language aside).
4451

4552
```rust
4653
impl Component for MyComponent {
@@ -59,7 +66,11 @@ For usage details, check out [the `html!` guide](html.md).
5966

6067
### Rendered
6168

62-
The `rendered()` component lifecycle method is called after `view()` is processed and Yew has rendered your component, but before the browser refreshes the page. A component may wish to implement this method to perform actions that can only be done after the component has rendered elements. You can check whether this is the first time the component was rendered via the `first_render` parameter.
69+
The `rendered` component lifecycle method is called once `view` has been called and Yew has rendered
70+
the results to the DOM, but before the browser refreshes the page. This method is useful when you
71+
want to perform actions that can only be completed after the component has rendered elements. There
72+
is also a parameter called `first_render` which can be used to determine whether this function is
73+
being called on the first render, or instead a subsequent one.
6374

6475
```rust
6576
use stdweb::web::html_element::InputElement;
@@ -90,14 +101,17 @@ impl Component for MyComponent {
90101
```
91102

92103
:::tip note
93-
Note that this lifecycle method does not require an implementation and will do nothing by default
104+
Note that this lifecycle method does not require an implementation and will do nothing by default.
94105
:::
95106

96107
### Update
97108

98-
Components are dynamic and can register to receive asynchronous messages. The `update()` lifecycle method is called for each message. This allows the component to update itself based on what the message was, and determine if it needs to re-render itself. Messages can be triggered by HTML elements listeners or be sent by child components, Agents, Services, or Futures.
109+
Communication with components happens primarily through messages which are handled by the
110+
`update` lifecycle method. This allows the component to update itself
111+
based on what the message was, and determine if it needs to re-render itself. Messages can be sent
112+
by event listeners, child components, Agents, Services, or Futures.
99113

100-
Here's an example of what `update()` could look like:
114+
Here's an example of what an implementation of `update` could look like:
101115

102116
```rust
103117
pub enum Msg {
@@ -126,9 +140,11 @@ impl Component for MyComponent {
126140

127141
### Change
128142

129-
Components may be re-rendered by their parents. When this happens, they could receive new properties and choose to re-render. This design facilitates parent to child component communication through changed properties.
143+
Components may be re-rendered by their parents. When this happens, they could receive new properties
144+
and need to re-render. This design facilitates parent to child component communication by just
145+
changing the values of a property.
130146

131-
A typical implementation would look like:
147+
A typical implementation would look something like:
132148

133149
```rust
134150
impl Component for MyComponent {
@@ -147,7 +163,9 @@ impl Component for MyComponent {
147163

148164
### Destroy
149165

150-
After Components are unmounted from the DOM, Yew calls the `destroy()` lifecycle method to support any necessary clean up operations. This method is optional and does nothing by default.
166+
After Components are unmounted from the DOM, Yew calls the `destroy` lifecycle method; this is
167+
necessary if you need to undertake operations to clean up after earlier actions of a component
168+
before it is destroyed. This method is optional and does nothing by default.
151169

152170
## Associated Types
153171

@@ -162,13 +180,22 @@ impl Component for MyComponent {
162180
}
163181
```
164182

165-
`Message` represents a variety of messages that can be processed by the component to trigger some side effect. For example, you may have a `Click` message which triggers an API request or toggles the appearance of a UI component. It is common practice to create an enum called `Msg` in your component's module and use that as the message type in the component. It is common to shorten "message" to "msg".
183+
The `Message` type is used to send messages to a component after an event has taken place; for
184+
example you might want to undertake some action when a user clicks a button or scrolls down the
185+
page. Because components tend to have to respond to more than one event, the `Message` type will
186+
normally be an enum, where each variant is an event to be handled.
187+
188+
When organising your codebase, it is sensible to include the definition of the `Message` type in the
189+
same module in which your component is defined. You may find it helpful to adopt a consistent naming
190+
convention for message types. One option (though not the only one) is to name the types
191+
`ComponentNameMsg`, e.g. if your component was called `Homepage` then you might call the type
192+
`HomepageMsg`.
166193

167194
```rust
168195
enum Msg {
169196
Click,
197+
FormInput(String)
170198
}
171199
```
172200

173201
`Properties` represents the information passed to a component from its parent. This type must implements the `Properties` trait \(usually by deriving it\) and can specify whether certain properties are required or optional. This type is used when creating and updating a component. It is common practice to create a struct called `Props` in your component's module and use that as the component's `Properties` type. It is common to shorten "properties" to "props". Since props are handed down from parent components, the root component of your application typically has a `Properties` type of `()`. If you wish to specify properties for your root component, use the `App::mount_with_props` method.
174-

0 commit comments

Comments
 (0)