Quantcast
Channel: ReScript Forum - Latest posts
Viewing all articles
Browse latest Browse all 1759

Accessing and tweaking Dom elements (maybe related to type conversion?)

$
0
0

Thank you for your respond!!

Not sure what you mean here. That you’re not giving it a definition? That means it’s an abstract type that you can’t interact with indirectly. You have to write bindings to operate on it.

Yeah I mean no definition. I see, I understand that now.

JsxDOMStyle is used in the rescript-react bindings, and the much more common way of interacting with the DOM. Most people will very rarely interact with the DOM directly, which is why the types are so bare bones.

Make sense I see!

Mutable state is discouraged. Much easier to reason about your code if you avoid mutation. Do you really need it?

For this example, yes. I need to change the zIndex of the element so that it’s placed behind other things.

  1. The return type isn’t restricted by anything, and so will be inferred entirely based on how it’s used later.

I see. It’s the nature of @get_index / @set_index, right?
Because they seem to just access the raw external property from Javascript so they can’t really check for type.

Even if I define it as get: ('a, string)=>option<string> and I by mistake use it to get some weird property like get(someHtmlElement, "classList") I would get some weird DOMTokenList that ReScript thinks to be option<string>.

It doesn’t matter even if I make multiple @get_index and @set_index for different types right?

@get_index external getStrProp : ('a, string) => option<string> = ""
@get_index external getFloatProp : ('a, string) => option<float> = ""
// ....

It’s more so that the type notation is there so that you don’t pass its returned value to a wrong function later on OHH WAIT… I GET IT, IT’S ALWAYS ABOUT that. It’s about not using those returned value wrongly. The part where we get those value from JS is the unsafe part that we have to handle carefully to make sure rescript gets the right data type info.

And unnecessary when you have a type where you can access the field directly.

I wasn’t sure which way is more standard/safe to do things in rescript.
It seems in general I see the following ways:

For property:

  • use @get, @set tag to bind the property directly @get external style....="style"
  • use @get_index, @set_index to dynamically get property (with a rather unsound type problem)
  • make a type record with accessible field

For method

  • use @send
  • make a type record with a property as the function type

ReScript is fun, though the puzzle is at this point for me more about binding things the right ways than actually solving programming problems.

Just to make sure I’m doing it right: we are supposed to write the binding ourselves right? We don’t have a binding presets of common javascript functions as of now right?


Viewing all articles
Browse latest Browse all 1759

Trending Articles