Skip to content

Commit 60d9c4f

Browse files
committed
Improve doc to use AttrValue and &Props { ... }
1 parent 080d6f2 commit 60d9c4f

File tree

1 file changed

+58
-30
lines changed

1 file changed

+58
-30
lines changed

website/docs/concepts/function-components/properties.mdx

+58-30
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,14 @@ pub struct Props {
6060
}
6161

6262
#[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}</> }
6565
}
6666

6767
// Then supply the prop
6868
#[function_component]
6969
fn App() -> Html {
70-
html! {<HelloWorld is_loading={true} />}
70+
html! { <HelloWorld is_loading=true /> }
7171
}
7272

7373
```
@@ -91,7 +91,7 @@ fn HelloWorld() -> Html {
9191
// No props to supply
9292
#[function_component]
9393
fn App() -> Html {
94-
html! {<HelloWorld />}
94+
html! { <HelloWorld /> }
9595
}
9696

9797
```
@@ -126,8 +126,8 @@ pub struct Props {
126126
}
127127

128128
#[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 {
131131
html! { "Loading" }
132132
} else {
133133
html! { "Hello world" }
@@ -137,12 +137,12 @@ fn HelloWorld(props: &Props) -> Html {
137137
// Then use like this with default
138138
#[function_component]
139139
fn Case1() -> Html {
140-
html! {<HelloWorld />}
140+
html! { <HelloWorld /> }
141141
}
142142
// Or no override the default
143143
#[function_component]
144144
fn Case2() -> Html {
145-
html! {<HelloWorld is_loading={true} />}
145+
html! { <HelloWorld is_loading=true /> }
146146
}
147147
```
148148

@@ -158,26 +158,32 @@ use yew::{function_component, html, Html, Properties};
158158

159159
#[derive(Properties, PartialEq)]
160160
pub struct Props {
161+
#[prop_or_default]
162+
pub is_loading: bool,
161163
// highlight-start
162-
#[prop_or("Bob".to_string())]
164+
#[prop_or(AttrValue::Static("Bob"))]
163165
// highlight-end
164-
pub name: String,
166+
pub name: AttrValue,
165167
}
166168

167169
#[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+
}
170176
}
171177

172178
// Then use like this with default
173179
#[function_component]
174180
fn Case1() -> Html {
175-
html! {<HelloWorld />}
181+
html! { <Hello /> }
176182
}
177183
// Or no override the default
178184
#[function_component]
179185
fn Case2() -> Html {
180-
html! {<HelloWorld name={"Sam".to_string()} />}
186+
html! { <Hello name="Sam" /> }
181187
}
182188
```
183189

@@ -190,32 +196,38 @@ The function is called when no explicit value has been given for that attribute.
190196
```rust
191197
use yew::{function_component, html, Html, Properties};
192198

193-
fn create_default_name() -> String {
194-
"Bob".to_string()
199+
fn create_default_name() -> AttrValue {
200+
AttrValue::Static("Bob")
195201
}
196202

197203
#[derive(Properties, PartialEq)]
198204
pub struct Props {
205+
#[prop_or_default]
206+
pub is_loading: bool,
199207
// highlight-start
200208
#[prop_or_else(create_default_name)]
201209
// highlight-end
202-
pub name: String,
210+
pub name: AttrValue,
203211
}
204212

205213
#[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+
}
208220
}
209221

210222
// Then use like this with default
211223
#[function_component]
212224
fn Case1() -> Html {
213-
html! {<HelloWorld />}
225+
html! { <Hello /> }
214226
}
215227
// Or no override the default
216228
#[function_component]
217229
fn Case2() -> Html {
218-
html! {<HelloWorld name={"Sam".to_string()} />}
230+
html! { <Hello name="Sam" /> }
219231
}
220232
```
221233

@@ -243,13 +255,19 @@ use yew::{function_component, html, Html, Properties, props, virtual_dom::AttrVa
243255

244256
#[derive(Properties, PartialEq)]
245257
pub struct Props {
246-
#[prop_or(AttrValue::from("Bob"))]
258+
#[prop_or_default]
259+
pub is_loading: bool,
260+
#[prop_or(AttrValue::Static("Bob"))]
247261
pub name: AttrValue,
248262
}
249263

250264
#[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+
}
253271
}
254272

255273
#[function_component]
@@ -259,30 +277,40 @@ fn App() -> Html {
259277
Props {} // Notice we did not need to specify name prop
260278
};
261279
// highlight-end
262-
html! {<HelloWorld ..pre_made_props />}
280+
html! { <Hello ..pre_made_props /> }
263281
}
264282
```
265283

266-
## Automatically generate properties (autoprops)
284+
## Automatically generate properties (yew-autoprops)
267285

268286
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.
270289

271290
```rust
272291
use yew::prelude::*;
273292

274293
#[autoprops]
275294
#[function_component]
276295
fn Greetings(
277-
#[prop_or("Hello".into())]
296+
#[prop_or_default]
297+
is_loading: bool,
298+
#[prop_or(AttrValue::Static("Hello"))]
278299
message: &AttrValue,
279-
#[prop_or("World".into())]
300+
#[prop_or(AttrValue::Static("World"))]
280301
name: &AttrValue,
281302
) -> Html {
282-
html! {<>{message}{name}</>}
303+
if is_loading {
304+
html! { "Loading" }
305+
} else {
306+
html! { <>{message}{" "}{name}</> }
307+
}
283308
}
284309

285310
// 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.
286314
```
287315

288316
## Evaluation Order

0 commit comments

Comments
 (0)