|
| 1 | +--- |
| 2 | +title: Function Components |
| 3 | +sidebar_label: Introduction |
| 4 | +description: Introduction to function components |
| 5 | +--- |
| 6 | + |
| 7 | +:::warning |
| 8 | +We're still working on function components and hooks. They're not quite ready to be used yet. |
| 9 | +If you'd like to help out, take a look at the [project board](https://github.com/yewstack/yew/projects/3) for a list of things that still need to be done. |
| 10 | +::: |
| 11 | + |
| 12 | + |
| 13 | +Function components are a simplified version of normal components. |
| 14 | +They consist of a single function that receives props and determines what should be rendered by returning `Html`. |
| 15 | +Basically, it's a component that's been reduced to just the `view` method. |
| 16 | +On its own that would be quite limiting because you can only create pure components, but that's where hooks come in. |
| 17 | +Hooks allow function components to use state and other Yew features without implementing the `Component` trait. |
| 18 | + |
| 19 | +## Creating function components |
| 20 | + |
| 21 | +The easiest way to create a function component is to add the [`#[function_component]`](function-components/attribute.md) attribute to a function. |
| 22 | + |
| 23 | +```rust |
| 24 | +#[function_component(HelloWorld)] |
| 25 | +fn hello_world() -> Html { |
| 26 | + html! { "Hello world" } |
| 27 | +} |
| 28 | +``` |
| 29 | + |
| 30 | +### Under the hood |
| 31 | + |
| 32 | +Function components consists of two parts. |
| 33 | +First, the `FunctionProvider` trait which is comparable to the `Component` trait but it only has a single method called `run`. |
| 34 | +The second part is the `FunctionComponent` struct which wraps around the `FunctionProvider` type and turns it into an actual `Component`. |
| 35 | +The `#[function_component]` attribute essentially just implements `FunctionProvider` for you and exposes it wrapped in `FunctionComponent`. |
| 36 | + |
| 37 | +### Hooks |
| 38 | + |
| 39 | +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. |
| 40 | + |
| 41 | +#### Pre-defined Hooks |
| 42 | + |
| 43 | +Yew comes with the following predefined hooks: |
| 44 | +- [`use_state`](function-components/pre-defined-hooks.md#use_state) |
| 45 | +- [`use_ref`](function-components/pre-defined-hooks.md#use_ref) |
| 46 | +- [`use_reducer`](function-components/pre-defined-hooks.md#use_reducer) |
| 47 | +- [`use_reducer_with_init`](function-components/pre-defined-hooks.md#use_reducer_with_init) |
| 48 | +- [`use_effect`](function-components/pre-defined-hooks.md#use_effect) |
| 49 | +- [`use_effect_with_deps`](function-components/pre-defined-hooks.md#use_effect_with_deps) |
| 50 | + |
| 51 | +#### Custom Hooks |
| 52 | + |
| 53 | +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. |
0 commit comments