docs: add more docs

This commit is contained in:
Jonathan Kelley committed 2022-01-11 01:11:47 -05:00
1 parent bdf234d728
commit 1560e2daca
20 files changed
+259 -44

No files matched your search

+12 -33
View File
@@ -45,10 +45,21 @@ Finally, we can include this list in the final structure:
```rust
rsx!(
ul {
{name_list}
name_list
}
)
```
Or, we can include the iterator inline:
```rust
rsx!(
ul {
names.iter().map(|name| rsx!(
li { "{name}" }
))
}
)
```
The HTML-rendered version of this list would follow what you would expect:
```html
<ul>
@@ -59,38 +70,6 @@ The HTML-rendered version of this list would follow what you would expect:
</ul>
```
### Rendering our posts with a PostList component
Let's start by modeling this problem with a component and some properties.
For this example, we're going to use the borrowed component syntax since we probably have a large list of posts that we don't want to clone every time we render the Post List.
```rust
#[derive(Props, PartialEq)]
struct PostListProps<'a> {
posts: &'a [PostData]
}
```
Next, we're going to define our component:
```rust
fn App(cx: Scope<PostList>) -> Element {
cx.render(rsx!{
ul { class: "post-list",
// we can drop an iterator directly into our elements
cx.props.posts.iter().map(|post| rsx!{
Post {
title: post.title,
age: post.age,
original_poster: post.original_poster
}
})
}
})
}
```
## Filtering Iterators
Rust's iterators are extremely powerful, especially when used for filtering tasks. When building user interfaces, you might want to display a list of items filtered by some arbitrary check.