Skip to content

Commit

Permalink
hooks: add use_memo
Browse files Browse the repository at this point in the history
  • Loading branch information
glennsl committed Mar 26, 2022
1 parent c08b0db commit 9f6212b
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lib/hooks.ml
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,14 @@ let use_effect ~on ?(equal = ( = )) ?(release = Fun.const ()) acquire =
release ();
acquire ());
None)

let use_memo ~on:input ?(equal = ( = )) f =
let last_input = use_ref input in
let output = use_ref_lazy (fun () -> f input) in

if not (equal input !last_input) then begin
last_input := input;
output := f input
end;

!output
55 changes: 55 additions & 0 deletions test/test_ml.ml
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,59 @@ let test_hooks_use_effect_custom_equal () =
act (fun () -> React.Dom.render (C.make ~a:2 ~b:3 ()) (Html.element c));
assert_equal !count 2)

let test_hooks_use_memo () =
let count = ref 0 in
let module UseMemo = struct
let%component make ~a =
let result =
React.Hooks.use_memo ~on:a (fun a ->
incr count;
a ^ Int.to_string !count)
in
div [||] [ string result ]
end in
withContainer (fun c ->
act (fun () ->
React.Dom.render (UseMemo.make ~a:"foo" ()) (Html.element c));
assert_equal c##.textContent (Js.Opt.return (Js.string "foo1"));

act (fun () ->
React.Dom.render (UseMemo.make ~a:"foo" ()) (Html.element c));
assert_equal c##.textContent (Js.Opt.return (Js.string "foo1"));

act (fun () ->
React.Dom.render (UseMemo.make ~a:"bar" ()) (Html.element c));
assert_equal c##.textContent (Js.Opt.return (Js.string "bar2"));

act (fun () ->
React.Dom.render (UseMemo.make ~a:"foo" ()) (Html.element c));
assert_equal c##.textContent (Js.Opt.return (Js.string "foo3")))

let test_hooks_use_memo_custom_equal () =
let count = ref 0 in
let module UseMemo = struct
let%component make ~a ~b =
let equal a b = fst a = fst b in
let result =
React.Hooks.use_memo ~on:(a, b) ~equal (fun (a, b) ->
incr count;
a ^ b ^ Int.to_string !count)
in
div [||] [ string result ]
end in
withContainer (fun c ->
act (fun () ->
React.Dom.render (UseMemo.make ~a:"foo" ~b:"x" ()) (Html.element c));
assert_equal c##.textContent (Js.Opt.return (Js.string "foox1"));

act (fun () ->
React.Dom.render (UseMemo.make ~a:"foo" ~b:"y" ()) (Html.element c));
assert_equal c##.textContent (Js.Opt.return (Js.string "foox1"));

act (fun () ->
React.Dom.render (UseMemo.make ~a:"bar" ~b:"y" ()) (Html.element c));
assert_equal c##.textContent (Js.Opt.return (Js.string "bary2")))

let test_use_effect_always () =
let count = ref 0 in
let module C = struct
Expand Down Expand Up @@ -842,6 +895,8 @@ let hooks =
; "use_effect" >::: [ "release" >:: test_hooks_use_effect_release ]
; "use_effect"
>::: [ "custom equal" >:: test_hooks_use_effect_custom_equal ]
; "use_memo" >::: [ "basic" >:: test_hooks_use_memo ]
; "use_memo" >::: [ "custom equal" >:: test_hooks_use_memo_custom_equal ]
]

let use_effect =
Expand Down

0 comments on commit 9f6212b

Please sign in to comment.