mirror of
https://github.com/DioxusLabs/dioxus
synced 2024-11-26 06:00:21 +00:00
add a warning about using use_hook(RefCell) in the docs (#3151)
This commit is contained in:
parent
37a6e9200f
commit
caa9759e44
2 changed files with 80 additions and 0 deletions
|
@ -222,6 +222,46 @@ pub fn remove_future(id: Task) {
|
|||
/// In order to clean up resources you would need to implement the [`Drop`] trait for an inner value stored in a RC or similar (Signals for instance),
|
||||
/// as these only drop their inner value once all references have been dropped, which only happens when the component is dropped.
|
||||
///
|
||||
/// <div class="warning">
|
||||
///
|
||||
/// `use_hook` is not reactive. It just returns the value on every render. If you need state that will track changes, use [`use_signal`](dioxus::prelude::use_signal) instead.
|
||||
///
|
||||
/// ❌ Don't use `use_hook` with `Rc<RefCell<T>>` for state. It will not update the UI and other hooks when the state changes.
|
||||
/// ```rust
|
||||
/// use dioxus::prelude::*;
|
||||
/// use std::rc::Rc;
|
||||
/// use std::cell::RefCell;
|
||||
///
|
||||
/// pub fn Comp() -> Element {
|
||||
/// let count = use_hook(|| Rc::new(RefCell::new(0)));
|
||||
///
|
||||
/// rsx! {
|
||||
/// button {
|
||||
/// onclick: move |_| *count.borrow_mut() += 1,
|
||||
/// "{count.borrow()}"
|
||||
/// }
|
||||
/// }
|
||||
/// }
|
||||
/// ```
|
||||
///
|
||||
/// ✅ Use `use_signal` instead.
|
||||
/// ```rust
|
||||
/// use dioxus::prelude::*;
|
||||
///
|
||||
/// pub fn Comp() -> Element {
|
||||
/// let mut count = use_signal(|| 0);
|
||||
///
|
||||
/// rsx! {
|
||||
/// button {
|
||||
/// onclick: move |_| count += 1,
|
||||
/// "{count}"
|
||||
/// }
|
||||
/// }
|
||||
/// }
|
||||
/// ```
|
||||
///
|
||||
/// </div>
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```rust, no_run
|
||||
|
|
|
@ -367,6 +367,46 @@ impl Scope {
|
|||
/// In order to clean up resources you would need to implement the [`Drop`] trait for an inner value stored in a RC or similar (Signals for instance),
|
||||
/// as these only drop their inner value once all references have been dropped, which only happens when the component is dropped.
|
||||
///
|
||||
/// <div class="warning">
|
||||
///
|
||||
/// `use_hook` is not reactive. It just returns the value on every render. If you need state that will track changes, use [`use_signal`](dioxus::prelude::use_signal) instead.
|
||||
///
|
||||
/// ❌ Don't use `use_hook` with `Rc<RefCell<T>>` for state. It will not update the UI and other hooks when the state changes.
|
||||
/// ```rust
|
||||
/// use dioxus::prelude::*;
|
||||
/// use std::rc::Rc;
|
||||
/// use std::cell::RefCell;
|
||||
///
|
||||
/// pub fn Comp() -> Element {
|
||||
/// let count = use_hook(|| Rc::new(RefCell::new(0)));
|
||||
///
|
||||
/// rsx! {
|
||||
/// button {
|
||||
/// onclick: move |_| *count.borrow_mut() += 1,
|
||||
/// "{count.borrow()}"
|
||||
/// }
|
||||
/// }
|
||||
/// }
|
||||
/// ```
|
||||
///
|
||||
/// ✅ Use `use_signal` instead.
|
||||
/// ```rust
|
||||
/// use dioxus::prelude::*;
|
||||
///
|
||||
/// pub fn Comp() -> Element {
|
||||
/// let mut count = use_signal(|| 0);
|
||||
///
|
||||
/// rsx! {
|
||||
/// button {
|
||||
/// onclick: move |_| count += 1,
|
||||
/// "{count}"
|
||||
/// }
|
||||
/// }
|
||||
/// }
|
||||
/// ```
|
||||
///
|
||||
/// </div>
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```rust
|
||||
|
|
Loading…
Reference in a new issue