简体   繁体   中英

how to render an array of JSX using .map in react

I have an array of objects like the following:


const features = [
{
        name: "Sponsor Block",
        images: [sponsorBlock1, sponsorBlock2],
        data: [
            `Uses the API found ${<a href='example.com/'>here</a>}. You can find more information on how it works`,
            "Lorem ipsum ........"
        ],
    },
]

In the first point you can see that I am using a template string to inject JSX (I'm not sure if this is the right way to do this)

I want to map (loop) over the features array and display the first point with the anchor tag in it, and the other points that are just text as normal text.

But when I loop over the data key in the first object I get this displayed in my JSX-> [Object][Object]

Is there a way how I can solve this while still keeping it inside the array?

Why not simply An array of fragment components

data:[
    <>Uses the API found <a href='example.com/'>here</a>. You can find more information on how it works</>,
    <>Lorem ipsum ........</>
]

Then when you render do:

return (
    <ul>
        {features.data.map((d,i)=><li key={i}>{d}</li>}
    </ul>
)
const features = [ { name: "Sponsor Block", images: [sponsorBlock1, sponsorBlock2], data: [ `Uses the API found <a href='example.com/'>here</a>. You can find more information on how it works`, "Lorem ipsum ........" ], }, ] features.map(feature => { return <div dangerouslySetInnerHTML={{ __html: feature.data }} /> })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM