@@ -60,14 +60,14 @@ pub struct Props {
60
60
}
61
61
62
62
#[function_component]
63
- fn HelloWorld (props : & Props ) -> Html {
64
- html! { <>{" Am I loading? - " }{props . is_loading. clone () }</ > }
63
+ fn HelloWorld (& Props { is_loading } : & Props ) -> Html {
64
+ html! { <>{" Am I loading? - " }{is_loading }</ > }
65
65
}
66
66
67
67
// Then supply the prop
68
68
#[function_component]
69
69
fn App () -> Html {
70
- html! {<HelloWorld is_loading = { true } / >}
70
+ html! { <HelloWorld is_loading = true / > }
71
71
}
72
72
73
73
```
@@ -91,7 +91,7 @@ fn HelloWorld() -> Html {
91
91
// No props to supply
92
92
#[function_component]
93
93
fn App () -> Html {
94
- html! {<HelloWorld / >}
94
+ html! { <HelloWorld / > }
95
95
}
96
96
97
97
```
@@ -126,8 +126,8 @@ pub struct Props {
126
126
}
127
127
128
128
#[function_component]
129
- fn HelloWorld (props : & Props ) -> Html {
130
- if props . is_loading. clone () {
129
+ fn HelloWorld (& Props { is_loading } : & Props ) -> Html {
130
+ if is_loading {
131
131
html! { " Loading" }
132
132
} else {
133
133
html! { " Hello world" }
@@ -137,12 +137,12 @@ fn HelloWorld(props: &Props) -> Html {
137
137
// Then use like this with default
138
138
#[function_component]
139
139
fn Case1 () -> Html {
140
- html! {<HelloWorld / >}
140
+ html! { <HelloWorld / > }
141
141
}
142
142
// Or no override the default
143
143
#[function_component]
144
144
fn Case2 () -> Html {
145
- html! {<HelloWorld is_loading = { true } / >}
145
+ html! { <HelloWorld is_loading = true / > }
146
146
}
147
147
```
148
148
@@ -158,26 +158,32 @@ use yew::{function_component, html, Html, Properties};
158
158
159
159
#[derive(Properties , PartialEq )]
160
160
pub struct Props {
161
+ #[prop_or_default]
162
+ pub is_loading : bool ,
161
163
// highlight-start
162
- #[prop_or(" Bob" . to_string( ))]
164
+ #[prop_or(AttrValue :: Static ( " Bob" ))]
163
165
// highlight-end
164
- pub name : String ,
166
+ pub name : AttrValue ,
165
167
}
166
168
167
169
#[function_component]
168
- fn HelloWorld (props : & Props ) -> Html {
169
- html! {<>{" Hello world" }{props . name. clone ()}</ >}
170
+ fn Hello (& Props { is_loading , ref name }: & Props ) -> Html {
171
+ if is_loading {
172
+ html! { " Loading" }
173
+ } else {
174
+ html! { <>{" Hello " }{name } </ >}
175
+ }
170
176
}
171
177
172
178
// Then use like this with default
173
179
#[function_component]
174
180
fn Case1 () -> Html {
175
- html! {< HelloWorld / >}
181
+ html! { < Hello / > }
176
182
}
177
183
// Or no override the default
178
184
#[function_component]
179
185
fn Case2 () -> Html {
180
- html! {< HelloWorld name = { " Sam" . to_string ()} / >}
186
+ html! { < Hello name = " Sam" / > }
181
187
}
182
188
```
183
189
@@ -190,32 +196,38 @@ The function is called when no explicit value has been given for that attribute.
190
196
``` rust
191
197
use yew :: {function_component, html, Html , Properties };
192
198
193
- fn create_default_name () -> String {
194
- " Bob" . to_string ( )
199
+ fn create_default_name () -> AttrValue {
200
+ AttrValue :: Static ( " Bob" )
195
201
}
196
202
197
203
#[derive(Properties , PartialEq )]
198
204
pub struct Props {
205
+ #[prop_or_default]
206
+ pub is_loading : bool ,
199
207
// highlight-start
200
208
#[prop_or_else(create_default_name)]
201
209
// highlight-end
202
- pub name : String ,
210
+ pub name : AttrValue ,
203
211
}
204
212
205
213
#[function_component]
206
- fn HelloWorld (props : & Props ) -> Html {
207
- html! {<>{" Hello world" }{props . name. clone ()}</ >}
214
+ fn Hello (& Props { is_loading , ref name }: & Props ) -> Html {
215
+ if is_loading {
216
+ html! { " Loading" }
217
+ } else {
218
+ html! { <>{" Hello " }{name }</ > }
219
+ }
208
220
}
209
221
210
222
// Then use like this with default
211
223
#[function_component]
212
224
fn Case1 () -> Html {
213
- html! {< HelloWorld / >}
225
+ html! { < Hello / > }
214
226
}
215
227
// Or no override the default
216
228
#[function_component]
217
229
fn Case2 () -> Html {
218
- html! {< HelloWorld name = { " Sam" . to_string ()} / >}
230
+ html! { < Hello name = " Sam" / > }
219
231
}
220
232
```
221
233
@@ -243,13 +255,19 @@ use yew::{function_component, html, Html, Properties, props, virtual_dom::AttrVa
243
255
244
256
#[derive(Properties , PartialEq )]
245
257
pub struct Props {
246
- #[prop_or(AttrValue :: from(" Bob" ))]
258
+ #[prop_or_default]
259
+ pub is_loading : bool ,
260
+ #[prop_or(AttrValue :: Static (" Bob" ))]
247
261
pub name : AttrValue ,
248
262
}
249
263
250
264
#[function_component]
251
- fn HelloWorld (props : & Props ) -> Html {
252
- html! {<>{" Hello world" }{props . name. clone ()}</ >}
265
+ fn Hello (& Props { name }: & Props ) -> Html {
266
+ if is_loading {
267
+ html! { " Loading" }
268
+ } else {
269
+ html! { <>{" Hello " }{name }</ > }
270
+ }
253
271
}
254
272
255
273
#[function_component]
@@ -259,30 +277,40 @@ fn App() -> Html {
259
277
Props {} // Notice we did not need to specify name prop
260
278
};
261
279
// highlight-end
262
- html! {< HelloWorld .. pre_made_props / >}
280
+ html! { < Hello .. pre_made_props / > }
263
281
}
264
282
```
265
283
266
- ## Automatically generate properties (autoprops)
284
+ ## Automatically generate properties (yew- autoprops)
267
285
268
286
In order to streamline your development process, you can also use the macro
269
- ` #[autoprops] ` that will automatically generate the ` Properties ` struct for you.
287
+ ` #[autoprops] ` (from the crate ` yew-autoprops ` ) that will automatically
288
+ generate the ` Properties ` struct for you.
270
289
271
290
``` rust
272
291
use yew :: prelude :: * ;
273
292
274
293
#[autoprops]
275
294
#[function_component]
276
295
fn Greetings (
277
- #[prop_or (" Hello" . into ())]
296
+ #[prop_or_default ]
297
+ is_loading : bool ,
298
+ #[prop_or (AttrValue :: Static (" Hello" ))]
278
299
message : & AttrValue ,
279
- #[prop_or (" World" . into ( ))]
300
+ #[prop_or (AttrValue :: Static ( " World" ))]
280
301
name : & AttrValue ,
281
302
) -> Html {
282
- html! {<>{message }{name }</ >}
303
+ if is_loading {
304
+ html! { " Loading" }
305
+ } else {
306
+ html! { <>{message }{" " }{name }</ > }
307
+ }
283
308
}
284
309
285
310
// The properties struct "GreetingsProps" will be generated automatically.
311
+ //
312
+ // `is_loading` will be passed as value to the components while `message` and
313
+ // `name` will use references because of the leading `&` in the definition.
286
314
```
287
315
288
316
## Evaluation Order
0 commit comments