Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

147
Vistas
Can anyone help how i can write this line of code.I want use map in jsx

I want to use map in my Jsx in react and it has error because I use it with ternary expressions. Can anyone help me?

{tf > 0 ? {products.map(product => <CartProduct key={product.id} productData={product}/>)} : ''}
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You have error in your syntax. You should not use curly braces if you are returning directly. Curly {} braces are only used when you have some processing/calculations to do and then don't want to return anything or if return something and not the complete function which you wrote. For example, use curly braces here:

const temp = () => {
  let a=20;
  console.log('a',a);
};

Notice that I don't want to return anything in the function.

However, if you don't have anything to process or want to directly return what you are processing, use round () brackets.

const temp = () => {
  let a = 20;
  console.log('a', a);

  return (
    <div>
      <h1>Hi</h1> there
    </div>
  );
};

Notice that I am returning multiple lines above.

Also, if you have multiple lines of code to return, then use round () brackets, otherwise just return the value.

const temp = () => {
  let a = 20;
  console.log('a', a);

  return 'Hi';
};

In this ternary operator, you are returning products.map(... function and for each product, you are returning <CartProduct component. This is a single statement (since map is a function), so you don't need to use round () also. So, the correct way to write the code is:

{tf > 0 ? products.map((product) => <CartProduct key={product.id} productData={product} />) : ''}

However, if you want to use round () brackets for single-lined return statements, you can do that too.

const temp = () => {
    let a = 20;
    console.log('a', a);
    return ('Hi');
};

So, you can also rewrite your code as:

{tf > 0 ? (products.map((product) => <CartProduct key={product.id} productData={product} />)) : ''}

Both the ways are correct.

about 4 years ago · Juan Pablo Isaza Denunciar

0

{tf > 0 ? products.map(product => <CartProduct key={product.id} productData={product}/>) : null}

You need to remove block scope ({}) around the products.map call, and don't return empty string '' in JSX; instead return null.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda