Should I condense my hooks output array?
Hey guys,
so I have my useValidation function and it returns the following array.
So 5 items in the array, but it seems like generally speaking the first 2 are the ones most frequently used like so
Should I condense my 5 item array into something smaller like this
Or do you think its better to leave it as 5?
14 Replies
you can split into two blocks
or just return an object. I feel better knowing what I'm using when there are more than 2 things
I had considered that but I didn't want to complicate the potential for multiple useValidations in a component. Granted I'm not sure why you would do that lol but I didnt want to hamper potential use cases that I hadn't thought of
However in hindsight, I decided my long term plan is to aggregate slices together into one slice, so maybe an object output is the move
If anything this is the best reason to use an object, so that instead of destructing you can just assign it all to a variable and essentially namespace each validator’s stuff by the variable names
I'm not sure if I follow what you mean. What does having multiple useValidations is a component have to do with it returning an array or an object?
I just like object for #props > 2 because I get autocomplete and select what I need on the left side, instead of having to read what it returns and fish out specific index of the thing you are looking for
React Query does this too with number of prop it returns
name spacing things can also be done with array too you just return object as element
but like I said, why not just object
My concern was I wanted to enable people to do something like this if they ever had a need, but in hindsight, I actually don't intend to support this because its not likely that a user will do it. But I was thinking about this
With array destructuring, the end user isnt stuck with a hard coded name that I decided, instead they get to utilize something they selected. But yeah I think I'm gonna nix it favor of
Also, follow up question
For validation declaration, which feels more intuitive.
`
`
To simplify the question, should required be explicit or implicit.
you can rename them as follows, this is exactly how you use multiple useMutation/Query hooks of ReactQuery
I prefer .optional()
If you have it in the validator that is not marked optional, then it must be required.
This level of implicitness I don't feel like its abstracting much if at all.
please for the love of god don’t destructure and just give each object a name
lol I definitely dont intend too, I was just repeating the previous pattern for my question
good good
Maybe "exactly how you use" is strong opinion of mine, would you share why you are opposed to destructuring?
I'm guessing possibly more stack space, undefined(but we know the object shape here) variable, and clutter?
wondering if there is anything I'm missing
Destructuring some stuff is fine, but at Spacedrive we’ve pretty much banned destructuring useQuery/useMutation/useForm returnss
It’s bc of exactly what you did in that snippet, where you aliased each field to have a name specific to the validator it’s used for
When you only have one thing to destructure it’s fine, but when u destructure multiple things with the same fields and have to individually alias all of them it looks terrible
So we enforce that you just give the object a name and use that to namespace the fields, dotting in really isn’t that bad
In very limited circumstances we allow it (eg. satisfying eslint with useEffect dependency arrays) but in general we require that those things can’t be restructured
gotcha yeah I can imagine more than 2 things its better to do just dot
I kinda missed that last about the implicit statement. What about it seems like a bad/poor abstraction?
and what do you think could be done to improve it?