mirror of
https://github.com/yewstack/yew.git
synced 2025-12-08 21:26:25 +00:00
* Read through the docs and correct spelling and grammar * Run prettier * Apply review suggestions Co-authored-by: Muhammad Hamza <muhammadhamza1311@gmail.com> * adjust translation messages Co-authored-by: Muhammad Hamza <muhammadhamza1311@gmail.com>
50 lines
2.0 KiB
Plaintext
50 lines
2.0 KiB
Plaintext
---
|
|
title: 'Hooks'
|
|
slug: /concepts/function-components/hooks
|
|
---
|
|
|
|
## Hooks
|
|
|
|
Hooks are functions that let you store state and perform side-effects.
|
|
|
|
Yew comes with a few pre-defined Hooks. You can also create your own or discover many [community made hooks](/community/awesome#hooks).
|
|
|
|
## Rules of hooks
|
|
|
|
1. A hook function name always has to start with `use_`
|
|
2. Hooks can only be used in the following locations:
|
|
- Top level of a function / hook.
|
|
- Blocks inside a function / hook, given it's not already branched.
|
|
- In the condition of a top level `if` expression inside a function / hook.
|
|
- In the scrutinee of a top level `match` expression inside a function / hook.
|
|
3. Hooks must be called in the same order for every render. Returning early is only allowed when using [Suspense](../../suspense.mdx)
|
|
|
|
These rules are enforced by either compile time or run-time errors.
|
|
|
|
### Pre-defined Hooks
|
|
|
|
Yew comes with the following predefined Hooks:
|
|
|
|
- [`use_state`](./use-state.mdx)
|
|
- [`use_state_eq`](./use-state.mdx#use_state_eq)
|
|
- [`use_memo`](./use-memo.mdx)
|
|
- [`use_callback`](./use-callback.mdx)
|
|
- [`use_mut_ref`](./use-mut-ref.mdx)
|
|
- [`use_node_ref`](./use-node-ref.mdx)
|
|
- [`use_reducer`](./use-reducer.mdx)
|
|
- [`use_reducer_eq`](./use-reducer.mdx#use_reducer_eq)
|
|
- [`use_effect`](./use-effect.mdx)
|
|
- [`use_effect_with_deps`](./use-effect.mdx#use_effect_with_deps)
|
|
- [`use_context`](./use-context.mdx)
|
|
- [`use_force_update`](./use-force-update.mdx)
|
|
|
|
### Custom Hooks
|
|
|
|
There are cases where you want to define your own Hooks to encapsulate potentially stateful logic from a component into reusable functions.
|
|
See the [Defining custom hooks](concepts/function-components/hooks/custom-hooks.mdx#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.
|