Next.js Discord

Discord Forum

My component isn't returning the images

Answered
Pacific herring posted this in #help-forum
Open in Discord
Pacific herringOP
Hi, i'll add some context: I receive a string from the database which contains the file urls, then I parse it and try to render them in my page.

const Liveries = async (props: TLiveriesProps) => {
    const { data } = props;
    
    return (
        <div className="flex flex-col">
            {data.map((item, i) => {
                const previews = JSON.parse(item.preview);
                return (
                    <>
                        <a className="hover:bg-black hover:text-white">{item.name}</a>
                        {previews.forEach((previewImage: string, index: number) => {
                            <>
                                {console.log(previewImage)}
                                <p>{previewImage}</p>
                                <Image width={100} height={100} src={previewImage} alt="hi"/>
                            </>
                        })}
                    </>
                )
            })}
        </div>
    )
}


my issue is that the console logs the image urls but Image coomponent isn't rendering them, I don't get any errors or http requests at all.
Answered by Aleutian Tern
Don't use forEach when tried to map an array and return a React component.
ForEach return VOID. Use map instead
View full answer

21 Replies

Pacific herringOP
/models/skinname/preview.jpg
And its correct because if i manually add an Image componente outside the foreach with that url It works fine
and you dont get errors on the browser console right?
Pacific herringOP
No
can you inspect element the Image components and check if they have the correct links
Pacific herringOP
Fact is i cant inspect them because they dont render in.
I've checked requests too, no 404 or 200 requests
try removing the console log and just map over the previews
@Arinji try removing the console log and just map over the previews
Pacific herringOP
Still they dont render in
weird, no clue other than that, sorry
Mossyrose gall wasp
can you try for loop instead of forEach plz?
Aleutian Tern
Don't use forEach when tried to map an array and return a React component.
ForEach return VOID. Use map instead
Answer
Pacific herringOP
Ima try later
Prolly that is the fix
@Aleutian Tern Don't use forEach when tried to map an array and return a React component. ForEach return VOID. Use map instead
Pacific herringOP
Btw shouldnt i get the "this component isnt returning jsx" or something?
Aleutian Tern
Yes, check if your Eslint Extension is enabled, here my eslint complained about returning VOID.
Pacific herringOP
Alright
Mossyrose gall wasp
@Pacific herring i thought you said i tried map 😄