-
-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Design decision issue: useTranslation hook returning array vs object #712
Comments
that architectural decision was made by react: https://reactjs.org/docs/hooks-intro.html |
Hooks don't have to return an array – even built-in You can check out https://usehooks.com/ with a few examples, some of which returning arrays, some – objects. E.g. for |
|
Although these two hooks are common, I expect a whole swarm of other popular hooks out there, which won't be using this return pattern for a good reason. So I don't think there will be an expectation issue if a hook from Besides, if you look closer at |
🤷♂️there was a full 4 months of existing hook version - honestly not the time to change the API now. It's decided and will stay like it is. Just to late -> v10 is out...people using the alpha of v10 for long. Appreciate your feedback but it's set -> still you can create a own hook reusing our if you prefer another API. |
No worries. Just wanted to share thoughts. Sorry that I missed alphas – totally understand that this feedback would be much more relevant a few months back. |
Closed by #714 🎉 |
One point in favor of the array syntax is superior minification abilities. Object keys won't be minified in most cases, because the minifier probably isn't going to have enough information to ensure correctness during the transformation. Destructuring, however, doesn't suffer from this problem. Since it's the user giving out the names to the array elements, the minifier is free to rename them as it wishes. |
That's true. However given the already short names as |
TIL: you can add keyed values to a JS that's freaking cool! |
👋 I've just discovered v10 (the hooks one) and am pretty excited by it! Here's a question about one architectural decision that has come to my mind: Why is the result of useTranslation() an array rather than an object?
Arrays are good for things that can have arbitrary names, e.g.
[x, setX] = useState()
, which is not the case forreact-i18next
'suseTranslation()
. As a developer, I'd liket
to bet
in all components, same fori18n
. Doing something likeconst [ abc, def ] = useTranslation();
would mean the component's code is harder to understand by the developers, which a sign of not following a good practice. On the other hand, if the result of the hook is an object, I can doconst { i18n } = useTranslation()
, e.g. when I only need to know the current language in a component. No need to have[, i18n]
in this case.Alternatively, the hook could be like
@jamuhl If I've missed a conversation about the hook design, could please point me to one? Otherwise, what are your thoughts on the current hook design? How did you justify it?
The text was updated successfully, but these errors were encountered: