Skip to content

Commit

Permalink
Document anti-pattern regarding props (#2817)
Browse files Browse the repository at this point in the history
* Document anti-pattern regarding props

* fmt

* Review

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

* make prettier happy

Co-authored-by: WorldSEnder <WorldSEnder@users.noreply.github.com>
  • Loading branch information
hamza1311 and WorldSEnder committed Aug 7, 2022
1 parent d422b53 commit a9a7a39
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions website/docs/concepts/function-components/properties.mdx
Expand Up @@ -262,3 +262,21 @@ fn App() -> Html {
html! {<HelloWorld ..pre_made_props />}
}
```

## Anti Patterns

While almost any Rust type can be passed as properties, there are some anti-patterns that should be avoided.
These include, but are not limited to:

1. Using `String` type instead of `AttrValue`. <br />
**Why is this bad?** `String` can be expensive to clone.
Cloning is often needed when the prop value is used with hooks and callbacks. `AttrValue` is either
a reference-counted string (`Rc<str>`) or a `&'static str`, thus very cheap to clone.<br />
**Note**: `AttrValue` internally is `IString` from [implicit-clone](https://crates.io/crates/implicit-clone)
See that crate to learn more.
2. Using interior mutability. <br />
**Why is this bad?** Interior mutability (such as with `RefCell`, `Mutex`, etc.) should
_generally_ be avoided. It can cause problems with re-renders (Yew doesn't know when state has changed)
so you may have to manually force a render. Like all things, it has its place. Use it with caution.
3. You tell us. Did you run into an edge-case you wish you knew about earlier? Feel free to create an issue
or PR a fix to this documentation.

0 comments on commit a9a7a39

Please sign in to comment.