Hacker News new | ask | show | jobs
by dmitriid 2551 days ago
So many frameworks do this, and I can’t understand why [1]:

    onClick: "checkTodoItem()"
Why? What compells people to go ahead and say: yes, we’re writing everything in JS/TS which has perfectly fine support for actual functions. That’s why we’re going to arbitrarily use strings that evaluate to function calls in some parts of our framework.

[1] See views https://typescene.dev/docs/introduction/overview

2 comments

The onClick event handler refers to a method on an object that exists when the component is rendered, not when the template is parsed by the JS interpreter. So, to refer to the method itself you'd need to do something crazy like go through .prototype or something.

Anyway, yes Typescene does let you supply an actual function in place of the string here, but for me that ruins the 'neat' flow of the view and starts mixing views with logic.

   onclick: () => function_to_be_defined_at_runtime()
This also lets you deal with functions that are defined at runtime. Moreover, this actually lets you provide types for the expected callbacks.

> but for me that ruins the 'neat' flow of the view and starts mixing views with logic.

How is providing a reference to a function "logic"? You still provide a reference to a method, only you do it in an unspecified stringly-typed DSL.

Be sides, you're using these magical strings in so, so many places: https://news.ycombinator.com/item?id=20310666

This is anything but strongly typed. This is stringly typed.

First time I've seen that (haven't used any JS frameworks in anger), this seems a little crazy, especially after espousing the benefits of TypeScript elsewhere. What other frameworks do this?