ml-in-barcelona / jsoo-react

js_of_ocaml bindings for ReactJS. Based on ReasonReact.
https://ml-in-barcelona.github.io/jsoo-react
MIT License
138 stars 19 forks source link

New updater functions being returned by useState on every render #100

Closed jchavarri closed 2 years ago

jchavarri commented 2 years ago

The generated code for useState is:

function useState(x278) {
  var
  ___ =
    caml_js_wrap_callback_strict
      (1, function (param) { return caml_call1(x278, 0) }),
  x279 = Ojs[18].React.useState(___);
  function _$_(x280) {
    var
    _aa_ =
      caml_js_wrap_callback_strict
        (1, function (x281) { return caml_call1(x280, x281) });
    caml_call2(Ojs[16], x279, 1)(_aa_);
    return 0
  }
  return [0, caml_call2(Ojs[16], x279, 0), _$_]
}

This is problematic because every time a new function _$_ is created, which breaks the assumptions of React, where if you do

let data, setData = React.useState (fun () -> 2)

setData should remain constant over time, but I am not sure it's possible as it'd require removing caml_js_wrap_callback_strict. I'm not sure why wrapping is needed for functions that only take 1 param 🤔