create
allows you to create your own primitives. init
is the initial value of the Interactive.t
. inject
is how the users of your primitive tell it what to do with new values. In your event handler, you should call it and supply the new value of the primitive.
You should use value
to determine the value of your primitive. For example, if your primitive is a checkbox, you should do this:
let render ~inject ~value =
let open Incr.Let_syntax in
let%map is_checked = value in
let node =
if is_checked
then (* ... create a [Node.t] representing a checked checkbox ... *)
else (* ... create a [Node.t] representing an unchecked checkbox ... *)
in
...
You should not use init
to determine the value. value
will initially be the same as init
, but it might be different if the field is modified, then stops being rendered, then is rendered again. In this case, we want the user's input to be preserved. But if you use init
to fill in the initial value, the user's input will be discarded in this case.
For an example, check the implementation of Primitives.text
.