You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Has there been a discussion about making hooks return object instead of array? The benefits behind that being:
// since typeguard doesn't work with destructuring assignment like this:constApp=()=>{const[match,params]=useRoute<{id: string}>('/path/:id')if(!match){return<div>notpresent</div>}return<div>{params.id}</div> // <- compiler think params may be null}// we have to write something like this:constApp=()=>{constroute=useRoute<{id: string}>('/path/:id')if(!route[0]){return<div>notpresent</div>}return<div>{route[1].id}</div>
// Not easily understandable what this index accessed variable is, at a glance. Though of course can be avoided// by assigning another variable with proper name or something, why not: ↓}// make it return object like this:constApp=()=>{constroute=useRoute<{id: string}>('/path/:id')if(!route.match){return<div>notpresent</div>}return<div>{route.params.id}</div>
}// by doing the same for useLocation, we don't have to introduce unused variable when using only one of the return values:constApp=()=>{const[_location,setLocation]=useLocation()// <- unused variablereturn<buttononClick={(e)=>setLocation('somepath')}>}constApp=()=>{const{setLocation}=useLocation()// <- no unused variablereturn<buttononClick={(e)=>setLocation('somepath')}>}
I'm assuming the upside of the current implementation is
closer signature to well known hooks like useState()
in useRoute case, some may think receiving array makes it more obvious that the second item params depends on the first item match, especially for non typescript users
Since this will cause a breaking change and even the subtle benefit isn't obvious for everybody, it might not get enough 👍s. But probably it's still worth discussing once for ppl wondering the same thing later.
(our workaround so far is using a wrapper hook that make it return an object, and also redefining most of return types)
The text was updated successfully, but these errors were encountered:
Thanks for the comment, and yea I guess so!
It's a valid decision to not make the change due to ROI, still wanted to make sure if there's any particular intention behind the current design and what other typescript users are feeling about etc.
molefrog
added
the
V3
Features that won't be released in v2, but planned for the next major release
label
Sep 14, 2023
molefrog
removed
the
V3
Features that won't be released in v2, but planned for the next major release
label
Mar 4, 2024
Has there been a discussion about making hooks return object instead of array? The benefits behind that being:
I'm assuming the upside of the current implementation is
useState()
useRoute
case, some may think receiving array makes it more obvious that the second itemparams
depends on the first itemmatch
, especially for non typescript usersSince this will cause a breaking change and even the subtle benefit isn't obvious for everybody, it might not get enough 👍s. But probably it's still worth discussing once for ppl wondering the same thing later.
(our workaround so far is using a wrapper hook that make it return an object, and also redefining most of return types)
The text was updated successfully, but these errors were encountered: