Search code examples
reactjsreact-fragment

Trying to add loop in function of React JS getting JSX expressions must have one parent element


react loop in function

I tried adding <> </> as mentioned in other answers but that dint work. Its a simple function component with return statement:

export default function Merchant4SubscriptionForm(props) {
//some code
return (
//some rendering
<div className="card active rounded bg-primary p-5 my-3 mr-3 text-center">
                            {produts.map(product => (
                                <h4>{product.id}</h4>
                                <h1 className="price">{product.price}</h1>
                                <ul className="no-disk">
                                    <li>{product.month_count} month count</li>
                                    <li>{product.time} Package</li>
                                    <li>{product.title}</li>
                                </ul>
                        ))} //This area shows red however, please check console attached.
                            <img className="img-responsive mt-5" src="2219 [Converted]-01.png" />
                        </div>

 );
}
}

Solution

  • The error got away with simple div added in the starting of the loop. Dint understand why though.

    {products.map(product => (
                                <div>
                                <h4>{product.id}</h4>
                                <h1 className="price">{product.price}</h1>
                                <ul className="no-disk">
                                    <li>{product.month_count} month count</li>
                                    <li>{product.time} Package</li>
                                    <li>{product.title}</li>
                                </ul>
                                </div>
                            ))}