-
-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
366 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
# Lifecycle | ||
|
||
Dioxus components can use hooks to manage certain lifecycle situations. | ||
|
||
## Component creation | ||
You can run certain logic when the component is created for the first time by using the `use_hook` hook. | ||
|
||
```rs | ||
fn app() -> Element { | ||
|
||
use_hook(|| { | ||
println!("Component running for the first time!"); | ||
}); | ||
|
||
rsx!(...) | ||
} | ||
``` | ||
|
||
## Component destroyment | ||
|
||
Run some logic when the component is being destroyed. | ||
|
||
```rs | ||
fn app() -> Element { | ||
|
||
use_drop(|| { | ||
println!("Component is being dropped."); | ||
}); | ||
|
||
rsx!(...) | ||
} | ||
``` | ||
|
||
## Side effects | ||
|
||
Run some logic when a signal is changed. | ||
|
||
```rs | ||
fn app() -> Element { | ||
let mut signal = use_signal(|| 1); | ||
|
||
use_effect(move || { | ||
// Because we are reading this signal now the effect is subscribed to any change | ||
let value = signal(); | ||
println!("Value of signal is {value}"); | ||
}); | ||
|
||
rsx!(...) | ||
} | ||
``` | ||
|
||
## Side effects with dependencies | ||
|
||
Run some logic when a signal is changed. | ||
|
||
```rs | ||
fn app() -> Element { | ||
let mut signal = use_signal(|| 1); | ||
let mut other_signal = use_signal(|| 1); | ||
|
||
// Manually specify non-signal values that we might want to react to | ||
use_effect(use_reactive(&signal, |value| { | ||
println!("Value of signal is {value}"); | ||
})); | ||
|
||
// When you need multiple values you can pass a tuple | ||
use_effect(use_reactive(&(signal, other_signal), |(value, other_signal)| { | ||
println!("Value of signals are {value} and {other_signal}"); | ||
})); | ||
|
||
rsx!(...) | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.