remove once, restore use_hook
This commit is contained in:
1 parent
36e9eb1160
commit
01726d6656
9 files changed
+19
-23
No files matched your search
@@ -15,7 +15,7 @@ use std::{
|
||||
|
||||
/// Provide an error boundary to catch errors from child components
|
||||
pub fn use_error_boundary() -> ErrorBoundary {
|
||||
once(|| provide_context(ErrorBoundary::new()))
|
||||
use_hook(|| provide_context(ErrorBoundary::new()))
|
||||
}
|
||||
|
||||
/// A boundary that will capture any errors from child components
|
||||
|
||||
@@ -92,15 +92,11 @@ pub fn remove_future(id: Task) {
|
||||
///
|
||||
/// // prints a greeting on the initial render
|
||||
/// pub fn use_hello_world() {
|
||||
/// once(|| println!("Hello, world!"));
|
||||
/// use_hook(|| println!("Hello, world!"));
|
||||
/// }
|
||||
/// ```
|
||||
pub fn once<State: Clone + 'static>(initializer: impl FnOnce() -> State) -> State {
|
||||
with_current_scope(|cx| cx.use_hook(initializer)).expect("to be in a dioxus runtime")
|
||||
}
|
||||
|
||||
pub fn use_hook<State: Clone + 'static>(initializer: impl FnOnce() -> State) -> State {
|
||||
once(initializer)
|
||||
with_current_scope(|cx| cx.use_hook(initializer)).expect("to be in a dioxus runtime")
|
||||
}
|
||||
|
||||
/// Get the current render since the inception of this component
|
||||
|
||||
@@ -81,7 +81,7 @@ fn events_generate() {
|
||||
// #[test]
|
||||
// fn components_generate() {
|
||||
// fn app() -> Element {
|
||||
// let render_phase = once(|| 0);
|
||||
// let render_phase = use_hook(|| 0);
|
||||
// *render_phase += 1;
|
||||
|
||||
// match *render_phase {
|
||||
|
||||
@@ -16,10 +16,10 @@ fn app_drops() {
|
||||
#[test]
|
||||
fn hooks_drop() {
|
||||
fn app() -> Element {
|
||||
once(|| String::from("asd"));
|
||||
once(|| String::from("asd"));
|
||||
once(|| String::from("asd"));
|
||||
once(|| String::from("asd"));
|
||||
use_hook(|| String::from("asd"));
|
||||
use_hook(|| String::from("asd"));
|
||||
use_hook(|| String::from("asd"));
|
||||
use_hook(|| String::from("asd"));
|
||||
|
||||
render! { div {} }
|
||||
}
|
||||
|
||||
@@ -18,7 +18,7 @@ fn test_memory_leak() {
|
||||
return render!({});
|
||||
}
|
||||
|
||||
let mut name = once(|| String::from("numbers: "));
|
||||
let mut name = use_hook(|| String::from("numbers: "));
|
||||
|
||||
name.push_str("123 ");
|
||||
|
||||
@@ -76,7 +76,7 @@ fn memo_works_properly() {
|
||||
return None;
|
||||
}
|
||||
|
||||
let name = once(|| String::from("asd"));
|
||||
let name = use_hook(|| String::from("asd"));
|
||||
|
||||
render!(
|
||||
div { "Hello, world! {name}" }
|
||||
@@ -117,7 +117,7 @@ fn free_works_on_root_hooks() {
|
||||
}
|
||||
|
||||
fn app(cx: AppProps) -> Element {
|
||||
let name: AppProps = once(|| cx.clone());
|
||||
let name: AppProps = use_hook(|| cx.clone());
|
||||
render!(child_component { inner: name.inner.clone() })
|
||||
}
|
||||
|
||||
@@ -146,14 +146,14 @@ fn supports_async() {
|
||||
let colors = use_signal(|| vec!["green", "blue", "red"]);
|
||||
let padding = use_signal(|| 10);
|
||||
|
||||
once(|| {
|
||||
use_hook(|| {
|
||||
spawn(async move {
|
||||
sleep(Duration::from_millis(1000)).await;
|
||||
colors.with_mut(|colors| colors.reverse());
|
||||
})
|
||||
});
|
||||
|
||||
once(|| {
|
||||
use_hook(|| {
|
||||
spawn(async move {
|
||||
sleep(Duration::from_millis(10)).await;
|
||||
padding.with_mut(|padding| {
|
||||
|
||||
@@ -9,7 +9,7 @@ async fn it_works() {
|
||||
static POLL_COUNT: AtomicUsize = AtomicUsize::new(0);
|
||||
|
||||
fn app() -> Element {
|
||||
once(|| {
|
||||
use_hook(|| {
|
||||
spawn(async {
|
||||
for x in 0..10 {
|
||||
tokio::time::sleep(Duration::from_micros(50)).await;
|
||||
|
||||
Reference in new issue
Block a user