solidjs / solid

A declarative, efficient, and flexible JavaScript library for building user interfaces.
https://solidjs.com
MIT License
32.17k stars 918 forks source link

Enhancement - Prop destructuring #2151

Closed pureliani closed 5 months ago

pureliani commented 5 months ago

Prop destructuring is supported natively in JavaScript, and developers, often ones migrating from react to solid, are used to this pattern. Supporting it in SolidJS would lower the barrier to entry for newer developers.

This is personal opinion, but destructuring props directly in the function signature is more readable, developers can immediately see which props a component expects and uses.

It might even be a slight optimization, destructuring once at the beginning of a function can help avoid repetitive access to the props object which is slightly slower.

And lastly, many IDEs and linters have optimizations and features that work well with destructuring, such as better autocompletion, checking for unused properties, type checking, and refactoring tools.

Personally i think the two big reasons are - lower barrier of entry and ease of use

deluksic commented 5 months ago

If this was easy to do, it would have been the default. The problem with it is that it doesn't work with reactivity where "reading is subscribing". This relies on getters. You can't implement the subscribe part if you destructure, as the component function body never reruns.

There is a community driven props destructuring plugin that rewrites your code, but I don't know how good that works. I feel like less compiler magic is better, but you have options for sure.

pureliani commented 5 months ago

If this was easy to do, it would have been the default. The problem with it is that it doesn't work with reactivity where "reading is subscribing". This relies on getters. You can't implement the subscribe part if you destructure, as the component function body never reruns.

There is a community driven props destructuring plugin that rewrites your code, but I don't know how good that works. I feel like less compiler magic is better, but you have options for sure.

Yes, i know that reactivity is lost when destructuring, but with the help of a compiler i don't think this would be an issue, i think this is the plugin which already does it, and to me this doesn't feel like compiler magic, more like, smoothening sharp edges of solidjs to make it look more like regular js

deluksic commented 5 months ago

Imo its magic because it (seems to) only work when having annotations on your functions which make them components. It is already pretty hard to get people who are used to react to understand reactivity. Now if you say, sometimes destructuring works, but only if you have this special annotation, that will be even more confusing. Personally, I would support this being core only if you can globally sove for this.

P.S. this would be better as a discussion than an issue. As it has been brought up many times.

ryansolid commented 5 months ago

I agree. If anything this is better as a discussion. As @deluksic unless there was some unavoidable and automatic way to identify components this would never work. And more over it would take more than just handling function signatures if you wanted any sort of consistency. The complexity there is hard to merit.

Not that we are at that state now anyway. If we were to try to implement this it would make sense only to people already well aware of the problem, making the solution not any better than the problem as it interfere with teaching. Like why does certain things work and not others. It again requires too much specific knowledge.