withastro / astro

The web framework for content-driven websites. ⭐️ Star to support our work!
https://astro.build
Other
44.07k stars 2.29k forks source link

Cannot destructure react component props #9110

Open izmttk opened 7 months ago

izmttk commented 7 months ago

Astro Info

Astro                    v3.5.4
Node                     v18.18.0
System                   Linux (x64)
Package Manager          unknown
Output                   static
Adapter                  none
Integrations             @astrojs/react

If this issue only occurs in one browser, which browser is a problem?

No response

Describe the Bug

When running astro dev, I got an error "Cannot destructure property 'xxx' of 'undefined' as it is undefined" when using the map method on React component props. It seems like this error has occurred before and has been resolved in #660 , but it's happening again. If I missed something, please let me know.

What's the expected result?

no errors

Link to Minimal Reproducible Example

https://stackblitz.com/edit/github-hrgerw

Participation

matthewp commented 7 months ago

Certainly seems straightfoward. Seems bad, but will need to take a closer look.

matthewp commented 7 months ago

This is related to not setting a key. Why this happens though is something I'm still investigating. But it's matter of missing the key, and it's giving you a bad error message.

matthewp commented 7 months ago

I think the way our code works is shadowing the warning from coming through. Looking into a fix.

izmttk commented 7 months ago

After adding a key prop, it works. Thanks a lot! :heart:

jhedstrom commented 6 months ago

This was quite the rabbit hole. Fixing/adding the key property anywhere a map method was being called resolved the issue, but the undefined error is very misleading 😬 The error also started seemingly at random, which was also very tricky to track down.

fbzz commented 5 months ago

+1, took me a long time to arrive here. Adding the key, worked.

Important mention: I was using on my index.astro, and it was only on the first render.

mattiaz9 commented 5 months ago

I just tried to migrate from v2 to v4 and i'm getting this error. Using key didn't solve it for me

mattiaz9 commented 5 months ago

I just tried to migrate from v2 to v4 and i'm getting this error. Using key didn't solve it for me

never mind, it was vite-plugin-svgr that was messing with astro somehow

ulysses-ck commented 4 months ago

After adding a key prop, it works. Thanks a lot! ❤️

What do you mean by adding a keyprop @izmttk ?

I already try to use a default value to the prop in the react component, but instead of giving the error is saying: " Astro detected an unhandled rejection. Here's the stack trace: TypeError: Cannot read properties of undefined (reading 'propNameExample')"

Edit: Nvm, I already realized it. I have an array where it is mapped into the component and does not have its own key to every item mapped.

matthewp commented 1 month ago

Is anyone still running into this issue? It seems cleared up when I use 4.8.

firxworx commented 2 weeks ago

@matthewp I just hit this running 4.10.3.

trace starts around @astrojs/react/server.js

I was using it with react-qr-code with its QRCode component inside another component that wraps it in a div.