diff --git a/ci/dictionary.txt b/ci/dictionary.txt index e9f0f98cf..122baa08a 100644 --- a/ci/dictionary.txt +++ b/ci/dictionary.txt @@ -2,6 +2,7 @@ personal_ws-1.1 en 0 utf-8 alloc ALLOC allocator +analagous Angular's asmjs binaryen @@ -13,6 +14,7 @@ cdylib charset codebase codegen +Component composable Config declaratively @@ -39,6 +41,7 @@ impl init INIT interop +interoperate json lifecycle Lifecycle @@ -78,5 +81,6 @@ Webpack WeeAlloc workspaces xmlns +Javascript yewtil Yewtil diff --git a/website/docs/concepts/agents.md b/website/docs/concepts/agents.md index 637096d84..49233ce97 100644 --- a/website/docs/concepts/agents.md +++ b/website/docs/concepts/agents.md @@ -3,9 +3,16 @@ title: "Agents" description: "Yew's Actor System" --- -Agents are similar to Angular's [Services](https://angular.io/guide/architecture-services) \(but without dependency injection\), and provide a Yew with an [Actor Model](https://en.wikipedia.org/wiki/Actor_model). Agents can be used to route messages between components independently of where they sit in the component hierarchy, or they can be used to create a shared state, and they can also be used to offload computationally expensive tasks from the main thread which renders the UI. There is also planned support for using agents to allow Yew applications to communicate across tabs \(in the future\). +Agents are similar to Angular's [Services](https://angular.io/guide/architecture-services) +\(but without dependency injection\), and provide Yew with an +[Actor Model](https://en.wikipedia.org/wiki/Actor_model). Agents can be used to route messages +between components independently of where they sit in the component hierarchy, or they can be used +to create shared state between different components. Agents can also be used to offload +computationally expensive tasks from the main thread which renders the UI. There is also planned +support for using agents to allow Yew applications to communicate across tabs \(in the future\). -In order for agents to run concurrently, Yew uses [web-workers](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers). +In order for agents to run concurrently, Yew uses +[web-workers](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers). ## Lifecycle @@ -42,8 +49,13 @@ A dispatcher allows uni-directional communication between a component and an age ## Overhead -Agents that live in their own separate web worker \(Private and Public\) incur serialization overhead on the messages they send and receive. They use [bincode](https://github.com/servo/bincode) to communicate with other threads, so the cost is substantially higher than just calling a function. Unless the cost of computation will outweigh the cost of message passing, you should contain your logic in the UI thread agents \(Job or Context\). +Agents that use web workers \(i.e. Private and Public\) will incur a serialization overhead on the +messages they send and receive. They use [bincode](https://github.com/servo/bincode) to communicate +with other threads, so the cost is substantially higher than just calling a function. Unless the +cost of computation will outweigh the cost of message passing, you should use agents running on the +UI thread \(i.e. Job or Context\). ## Further reading -* The [pub\_sub](https://github.com/yewstack/yew/tree/master/examples/pub_sub) example shows how components can use agents to communicate with each other. +* The [pub\_sub](https://github.com/yewstack/yew/tree/master/examples/pub_sub) example shows how +components can use agents to communicate with each other. diff --git a/website/docs/concepts/function-components.md b/website/docs/concepts/function-components.md index e0e4ccaee..d0795ca41 100644 --- a/website/docs/concepts/function-components.md +++ b/website/docs/concepts/function-components.md @@ -4,11 +4,12 @@ sidebar_label: Introduction description: "Introduction to function components " --- -Function components are a simplified version of normal components. -They consist of a single function that receives props and determines what should be rendered by returning `Html`. -Basically, it's a component that's been reduced to just the `view` method. -On its own that would be quite limiting because you can only create pure components, but that's where Hooks come in. -Hooks allow function components to use state and other Yew features without implementing the `Component` trait. +Function components are a simplified version of normal components. They consist of a single function +that receives props and determines what should be rendered by returning `Html`. Basically, it's a +component that's been reduced to just the `view` method. On its own that would be quite limiting +because you can only create pure components, but that's where Hooks come in. Hooks allow function +components to maintain their own internal state and use other Yew features without needing to manually +implement the `Component` trait. ## Creating function components @@ -25,14 +26,19 @@ fn hello_world() -> Html { ### Under the hood -Function components consists of two parts. -First, the `FunctionProvider` trait which is comparable to the `Component` trait but it only has a single method called `run`. -The second part is the `FunctionComponent` struct which wraps around the `FunctionProvider` type and turns it into an actual `Component`. -The `#[function_component]` attribute essentially just implements `FunctionProvider` for you and exposes it wrapped in `FunctionComponent`. +There are two parts to how Yew implements function components. + +The first part is the `FunctionProvider` trait which is analogous to the `Component` trait, except +that it only has a single method (called `run`). The second part is the `FunctionComponent` struct +which wraps types implementing `FunctionProvider` and implements `Component`. + +The `#[function_component]` attribute is a procedural macro which automatically implements +`FunctionProvider` for you and exposes it wrapped in `FunctionComponent`. ### Hooks -Hooks are simply functions that let you “hook into” components' state and/or lifecycle and perform actions. Yew comes with a few pre-defined Hooks. You can also create your own. +Hooks are functions that let you "hook into" components' state and/or lifecycle and perform +actions. Yew comes with a few pre-defined Hooks. You can also create your own. #### Pre-defined Hooks @@ -48,3 +54,8 @@ Yew comes with the following predefined Hooks: There are cases where you want to define your own Hooks for reasons. Yew allows you to define your own Hooks which lets you extract your potentially stateful logic from the component into reusable functions. See the [Defining custom hooks](function-components/custom-hooks.md#defining-custom-hooks) section for more information. + +## Further reading + +* The React documentation has a section on [React hooks](https://reactjs.org/docs/hooks-intro.html). +These are not exactly the same as Yew's hooks, but the underlying concept is similar. diff --git a/website/docs/concepts/html/lists.md b/website/docs/concepts/html/lists.md index 75cadae16..503d97b49 100644 --- a/website/docs/concepts/html/lists.md +++ b/website/docs/concepts/html/lists.md @@ -4,7 +4,8 @@ title: "Lists" ## Fragments -The `html!` macro always requires a single root node. In order to get around this restriction, it's valid to wrap content in empty tags: +The `html!` macro always requires a single root node. In order to get around this restriction, you +can use an "empty tag" (these are also called "fragments"). @@ -35,7 +36,10 @@ html! { ## Iterators -Yew supports two different syntaxes for building html from an iterator: +Yew supports two different syntaxes for building HTML from an iterator. + +The first is to call `collect::()` on the final transform in your iterator, which returns a +list that Yew can display. @@ -51,6 +55,9 @@ html! { }; ``` +The alternative is to use the `for` keyword, which is not native Rust syntax and instead is used by +the HTML macro to output the needed code to display the iterator. + ```rust use yew::{html}; @@ -65,7 +72,7 @@ html! { ``` -## Relevant examples +## Further reading - [TodoMVC](https://github.com/yewstack/yew/tree/master/examples/todomvc) -- [Keyed List](https://github.com/yewstack/yew/tree/master/examples/keyed_list) +- [Keyed list](https://github.com/yewstack/yew/tree/master/examples/keyed_list) - [Router](https://github.com/yewstack/yew/tree/master/examples/router) diff --git a/website/versioned_docs/version-0.18.0/concepts/html.md b/website/versioned_docs/version-0.18.0/concepts/html.md index 8fca5e84e..a29e56c39 100644 --- a/website/versioned_docs/version-0.18.0/concepts/html.md +++ b/website/versioned_docs/version-0.18.0/concepts/html.md @@ -9,20 +9,32 @@ The `html!` macro allows you to write HTML and SVG code declaratively. It is sim **Important notes** -1. The `html!` macro only accepts one root html node \(you can counteract this by +1. The `html!` macro only accepts a single root HTML node \(this obstacle is easily overcome by [using fragments or iterators](html/lists.md)\) 2. An empty `html! {}` invocation is valid and will not render anything -3. Literals must always be quoted and wrapped in braces: `html! { "Hello, World" }` +3. Literals must always be wrapped in quotes as well as braces (i.e. +`html! {
{"Hello, World"}
}` is valid, but not `html! {Hello, World
}` or +`html! {"Hello, World"
}`). :::note -The `html!` macro can reach the default recursion limit of the compiler. If you encounter compilation errors, add an attribute like `#![recursion_limit="1024"]` in the crate root to overcome the problem. +The requirement to need braces and quotes was not a deliberate design choice (just in case you're +wondering)! It's needed in order to make parsing the tokens fed into the `html!` macro possible. +::: + +:::note +The `html!` macro can cause problems because it makes a lot of recursive calls. This means that it +can exceed the default recursion limit of the compiler. If you encounter a compilation error +(which might say something about "overflow" or "recursion limit reached") adding an attribute like +`#![recursion_limit="1024"]` to your crate root should fix the problem. ::: ## Tag Structure -Tags are based on HTML tags. Components, Elements, and Lists are all based on this tag syntax. +Tags inside the `html!` macros are heavily inspired by HTML tags. Components, elements, and lists +all use the tag syntax. -Tags must either self-close `<... />` or have a corresponding end tag for each start tag. +Every tag must either either close itself (e.g. `