Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

If condition inside of map() React JS

I’m learning reactjs and I want to change the color of a badge component depending on the value of a record that I call from the database through an API. However, when I try to do this it shows me nothing when I run it, do you know what I am missing or how I can do it?

<tbody>
  {this.state.SociosData.map((e, key) => {
    return (
      <tr className="trTable">
        <td>
          <b>{e.nombre_tienda}</b>{" "}
          <p className="mt-3  ">
            {e.id_tienda} | {e.tipo_tienda} | Area {e.area_tienda}
          </p>
          <p>{e.direccion_tienda}</p>
        </td>
        <td>
          {() => {
            if (e.estatusfirma_tienda === "Aprobado") {
              return <Badge bg="success">Firmado</Badge>;
            } else if (e.estatusfirma_tienda === "Rechazado") {
              return <Badge bg="danger">Firmado</Badge>;
            } else {
              return <Badge bg="warning">Firmado</Badge>;
            }
          }}
          {() => {
            if (e.estadolinea_tienda === "Activo") {
              return <Badge bg="success">Activo</Badge>;
            } else {
              return (
                <Badge bg="light" text="dark">
                  Inactivo
                </Badge>
              );
            }
          }}
        </td>
        <ModalFirma></ModalFirma>
      </tr>
    );
  })}
</tbody>

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Your problem is that you are running you’re if statement as a function. As @Nicholas Tower mentioned you would have to wrap it and invoke it.

I would make a ternary operator in your badge prop that will check the status and change accordingly.

<Badge bg={e.estatusfirma_tienda === 'Aprobado' ? 'success' : e.estatusfirma_tienda === 'Rechazado' ? 'danger' : 'warning'}>Firmado</Badge>

I tested it myself and confirm this works.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading