You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/concepts/components.md
+39-12Lines changed: 39 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,7 +4,9 @@ description: Components and their lifecycle hooks
4
4
---
5
5
## What are Components?
6
6
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.
8
10
9
11
## Lifecycle
10
12
@@ -18,7 +20,8 @@ Components are the building blocks of Yew. They manage their own state and can r
18
20
19
21
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.
20
22
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:
22
25
23
26
```rust
24
27
pubstructMyComponent {
@@ -40,7 +43,11 @@ impl Component for MyComponent {
40
43
41
44
### View
42
45
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).
44
51
45
52
```rust
46
53
implComponentforMyComponent {
@@ -59,7 +66,11 @@ For usage details, check out [the `html!` guide](html.md).
59
66
60
67
### Rendered
61
68
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.
63
74
64
75
```rust
65
76
usestdweb::web::html_element::InputElement;
@@ -90,14 +101,17 @@ impl Component for MyComponent {
90
101
```
91
102
92
103
:::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.
94
105
:::
95
106
96
107
### Update
97
108
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.
99
113
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:
101
115
102
116
```rust
103
117
pubenumMsg {
@@ -126,9 +140,11 @@ impl Component for MyComponent {
126
140
127
141
### Change
128
142
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.
130
146
131
-
A typical implementation would look like:
147
+
A typical implementation would look something like:
132
148
133
149
```rust
134
150
implComponentforMyComponent {
@@ -147,7 +163,9 @@ impl Component for MyComponent {
147
163
148
164
### Destroy
149
165
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.
151
169
152
170
## Associated Types
153
171
@@ -162,13 +180,22 @@ impl Component for MyComponent {
162
180
}
163
181
```
164
182
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`.
166
193
167
194
```rust
168
195
enumMsg {
169
196
Click,
197
+
FormInput(String)
170
198
}
171
199
```
172
200
173
201
`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.
0 commit comments