data becomes undefined when it is not undefined

I have a component which has some props. When I console log the props they work fine but when I access any fields of the props it becomes undefined
16 Replies
Valhalaar
Valhalaar2y ago
Looks like you have nested objects. data.data.whatever
aditya
aditya2y ago
I changes the function signature to this and it worked
aditya
aditya2y ago
why did this happen?
barry
barry2y ago
data.data.uid
Valhalaar
Valhalaar2y ago
Because you’re now destructuring the inner data object from props. So now you can reference just data.whatever If you don’t destructure the arguments then it’s what Barry and I said above
aditya
aditya2y ago
this happens if i dont destructure it because I declared the type I think
aditya
aditya2y ago
Valhalaar
Valhalaar2y ago
I think you’re confusing yourself potentially by calling props data
barry
barry2y ago
thats just your ts type breaking it data.data is of SentReceiptType
Valhalaar
Valhalaar2y ago
It’s not generally easy to think about data.data it’s far easier to think about props.data imo but yes the type is wrong
barry
barry2y ago
data is not you're breaking it
aditya
aditya2y ago
oh I see props is not data, got it Thanks!
Valhalaar
Valhalaar2y ago
You can call it whatever you want but personally I don’t like calling the top level object the same thing as the nested property. I find it hard to think about the structure when I do that Alternatively if you only have a single top level property in the props object you could make your life easier by spreading it and flattening it so you don’t have to think about a nested object in props, instead you just have a bunch of top-level properties
barry
barry2y ago
const { data: receipts, isLoading: receiptsLoading } = trpc.receiptRouter.getAll.useQuery();
const { data: receipts, isLoading: receiptsLoading } = trpc.receiptRouter.getAll.useQuery();
Is probably how I would name it. Something along those lines Never go generic
Valhalaar
Valhalaar2y ago
Yeah any specific name is typically better than data 100% agree
aditya
aditya2y ago
will do that! got it thanks! 😂 thats only thing that came to my mind
Want results from more Discord servers?
Add your server