Skip to content

Commit 39f8489

Browse files
committed
feat: now use_effect accepts JsValue as effect function
BREAKING CHANGE: use_effect* api changed
1 parent 60b7a35 commit 39f8489

File tree

1 file changed

+18
-17
lines changed

1 file changed

+18
-17
lines changed

src/use_effect.rs

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,27 @@ crate::macro_import::wasm_bindgen_react! {
55
///
66
/// # Safety
77
///
8-
/// `effect` closure will be passed to js runtime
9-
/// and called ( or never called ) asynchronously by React.
8+
/// `effect` should be a js function.
109
///
11-
/// As [`wasm_bindgen`](https://rustwasm.github.io/docs/wasm-bindgen/reference/passing-rust-closures-to-js.html) describes,
10+
/// If `effect` is the value returned from `Closure::once_into_js()`.
11+
/// You need to make sure `effect` changes if and only if
12+
/// `dependencies` change.
13+
/// If `dependencies` change but `effect` doesn't change,
14+
/// `effect` will be called more than once.
15+
/// If `effect` but `dependencies` doesn't change,
16+
/// `effect` will never be called causing memory leak.
1217
///
13-
/// > Once a Closure is dropped, it will deallocate its internal memory
14-
/// > and invalidate the corresponding JavaScript function
15-
/// > so that any further attempts to invoke it raise an exception.
16-
///
17-
/// Rust compiler can only ensure this closure is not dropped before `use_effect` returned,
18-
/// so it's up to you to make sure the closure lived longer enough
19-
/// to be valid in the component life time.
20-
///
21-
/// Thus, this function is marked `unsafe` as a warning.
2218
#[wasm_bindgen(js_namespace = React, js_name = useEffect)]
23-
pub unsafe fn use_effect(effect: &Closure<dyn FnMut()>, dependencies: Option<Box<[JsValue]>>);
19+
pub unsafe fn use_effect(effect: JsValue, dependencies: js_sys::Array);
2420

21+
/// `React.useEffect(effect)`
22+
///
23+
/// # Safety
24+
///
25+
/// `effect` should be a js function.
26+
///
27+
/// It can the value returned from `Closure::once_into_js()`.
28+
///
2529
#[wasm_bindgen(js_namespace = React, js_name = useEffect)]
26-
pub unsafe fn use_effect_with_clean(
27-
effect: &Closure<dyn FnMut() -> JsValue>,
28-
dependencies: Option<Box<[JsValue]>>,
29-
);
30+
pub unsafe fn use_effect_on_each_render(effect: JsValue);
3031
}

0 commit comments

Comments
 (0)