Zaid-Ajaj / Feliz

A fresh retake of the React API in Fable and a collection of high-quality components to build React applications in F#, optimized for happiness
https://zaid-ajaj.github.io/Feliz/
MIT License
540 stars 78 forks source link

Can we make [<Hook>] only change the function if it doesn't start with use? #479

Closed MangelMaxime closed 2 years ago

MangelMaxime commented 2 years ago

Hello,

in general when writing hook function, I took the habit of doing it like that:

[<Hook>]
let useAuth () =
    React.useContext(AuthContext)

because I was thinking that the [<Hook>] attributes was just "ensur[ing] the generated code starts with "use" in order for fast-refresh to pick it up" (quoted from the doc comment).

https://github.com/Zaid-Ajaj/Feliz/blob/4d621ebabfabef1723144ff155121397ce701e13/Feliz.CompilerPlugins/Hook.fs#L6-L7

But it is not really "ensuring" but always forcing it to start with use even if the name already start with use. So for example, the example I provided generates useuseAuth which looks really ugly hide a few characters to the generated JS.

If you think that the current behaviour is ok, does Hook attribute do something else beside a adding use in the beginning? If not, is it safe to assume that removing it in case would be safe?

I took the habit of writing it with the [<Hook>] because it make easy to know that this is hook. Kind of like it is easy that all functions starting with [<ReactComponent>] are react components.

Zaid-Ajaj commented 2 years ago

You are right @MangelMaxime, this is sub-optimal for sure. I've published a fix for this in Feliz v1.64 🚀 can you take it for a spin?

MangelMaxime commented 2 years ago

Thank you @Zaid-Ajaj, it looks good to me.