Advanced
Greg Skriloff@greg
1/15/2023

React devs - is there a best practice to rename properties from a hook? Say I have multiple useHooks: `const { data, isLoading } = useHook` Would you do this: `const { data: tx, isLoading: isTxLoading } = useHook` Or opposite: `const { data: tx, isLoading: TxIsLoading } = useHook` Or something else?

In reply to @greg
Goksu Toprak@gt
1/15/2023

You could follow the `useState` pattern and actually return an array: `const [tx, isLoading] = useHook()`. Wouldn't recommend if it going to be a lot more properties coming back from the hook and would just go with local re-naming like you did on your last example.

In reply to @greg
borodutch 💎🤲@borodutch
1/15/2023

just stay consistent

In reply to @greg
Dan Cortes@typeof
1/15/2023

I wouldn't call this a React best practice, necessarily, but I've always followed the convention that boolean variables are named "isFoo" or "hasFoo", so in this case, I'd do the former.

In reply to @greg
brian is not live@briang
1/16/2023

TxIsLoading is just worse naming convention imo isTxLoading sounds like a yes/no question which is what it should be as a boolean

In reply to @greg
Alex Loukissas@futureartist
1/16/2023

The former (isTxLoading). Unfortunately, we can't use ? in variable names, which makes naming booleans so obvious and easy to read. In e.g. elixir it's very common to write txLoading?

In reply to @greg
Chu Ka-Cheong@kc
1/16/2023

Both names make sense. I think a large part of naming is convention and consistency. If all your codebase follows either of the patterns I think it is fine.

In reply to @greg
Brandon@bleb
1/16/2023

‘const { data: tx, isLoading: isTxLoading } = useHook` 100%

In reply to @greg
Dan Cortes@typeof
1/16/2023

Another option is to skip destructuring altogether. Might be easier if you're working with multiple instances of the same hook.

In reply to @greg
Yash Karthik@yashkarthik
1/16/2023

2nd one.

In reply to @greg
Ertan Dogrultan@ertan
1/16/2023

not a react dev but these are the types of questions I usually ask chatgpt these days.

In reply to @greg
Cameron Armstrong@cameron
1/17/2023

ChatGPT says… “There is no one "best" practice for renaming properties from a hook. The most important thing is to choose a naming convention that makes sense for the specific context of your application, and to be consistent in your use of it throughout your codebase.”