switch to use_reactive
This commit is contained in:
1 parent
b8c41efb54
commit
df41df75d0
8 files changed
+138
-112
No files matched your search
@@ -2,8 +2,7 @@ use dioxus_core::prelude::*;
|
||||
use dioxus_signals::ReactiveContext;
|
||||
use futures_util::StreamExt;
|
||||
|
||||
use crate::use_signal;
|
||||
use dioxus_signals::*;
|
||||
use crate::use_callback;
|
||||
|
||||
/// `use_effect` will subscribe to any changes in the signal values it captures
|
||||
/// effects will always run after first mount and then whenever the signal values change
|
||||
@@ -22,11 +21,31 @@ use dioxus_signals::*;
|
||||
/// }
|
||||
/// }
|
||||
/// ```
|
||||
///
|
||||
/// ## With non-reactive dependencies
|
||||
/// To add non-reactive dependencies, you can use the `use_reactive` hook.
|
||||
///
|
||||
/// Signals will automatically be added as dependencies, so you don't need to call this method for them.
|
||||
///
|
||||
/// ```rust
|
||||
/// # use dioxus::prelude::*;
|
||||
/// # async fn sleep(delay: u32) {}
|
||||
///
|
||||
/// #[component]
|
||||
/// fn Comp(count: u32) -> Element {
|
||||
/// // Because the memo subscribes to `count` by adding it as a dependency, the memo will rerun every time `count` changes.
|
||||
/// use_effect(use_reactive((&count, |(count,)| println!("Manually manipulate the dom") )));
|
||||
///
|
||||
/// todo!()
|
||||
/// }
|
||||
/// ```
|
||||
#[track_caller]
|
||||
pub fn use_effect(mut callback: impl FnMut() + 'static) -> Effect {
|
||||
pub fn use_effect(callback: impl FnMut() + 'static) -> Effect {
|
||||
// let mut run_effect = use_hook(|| CopyValue::new(true));
|
||||
// use_hook_did_run(move |did_run| run_effect.set(did_run));
|
||||
|
||||
let callback = use_callback(callback);
|
||||
|
||||
let location = std::panic::Location::caller();
|
||||
|
||||
use_hook(|| {
|
||||
@@ -34,7 +53,7 @@ pub fn use_effect(mut callback: impl FnMut() + 'static) -> Effect {
|
||||
spawn(async move {
|
||||
loop {
|
||||
// Run the effect
|
||||
rc.run_in(&mut callback);
|
||||
rc.run_in(&*callback);
|
||||
|
||||
// Wait for context to change
|
||||
let _ = changed.next().await;
|
||||
@@ -54,37 +73,6 @@ pub struct Effect {
|
||||
}
|
||||
|
||||
impl Effect {
|
||||
/// Adds an explicit dependency to the effect. If the dependency changes, the effect's closure will rerun.
|
||||
///
|
||||
/// Signals will automatically be added as dependencies, so you don't need to call this method for them.
|
||||
///
|
||||
/// NOTE: You must follow the rules of hooks when calling this method.
|
||||
///
|
||||
/// ```rust
|
||||
/// # use dioxus::prelude::*;
|
||||
/// # async fn sleep(delay: u32) {}
|
||||
///
|
||||
/// #[component]
|
||||
/// fn Comp(delay: u32) -> Element {
|
||||
/// // Because the effect subscribes to `delay` by adding it as a dependency, the effect's closure will rerun every time `delay` changes.
|
||||
/// use_effect(move || {
|
||||
/// println!("It is safe to manually manipulate the dom here");
|
||||
/// })
|
||||
/// .use_dependencies((&delay,));
|
||||
///
|
||||
/// todo!()
|
||||
/// }
|
||||
/// ```
|
||||
pub fn use_dependencies(mut self, dependency: impl Dependency) -> Self {
|
||||
let mut dependencies_signal = use_signal(|| dependency.out());
|
||||
let changed = { dependency.changed(&*dependencies_signal.read()) };
|
||||
if changed {
|
||||
dependencies_signal.set(dependency.out());
|
||||
self.mark_dirty();
|
||||
}
|
||||
self
|
||||
}
|
||||
|
||||
/// Marks the effect as dirty, causing it to rerun on the next render.
|
||||
pub fn mark_dirty(&mut self) {
|
||||
self.rc.mark_dirty();
|
||||
|
||||
Reference in new issue
Block a user