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/else statements inside a function for react-table

Is it possible to add if/else logic in the iteration as I would like to add different class names depending on the value of paymentType?

This is used inside react-table. I’m unsure what the syntax is.

const PaymentTypes = ({ values }) => {
    // Iterate the array and create a badge-like component
    return (
        <>
            {values.map((paymentType, idx) => {
                return (
                    <span key={idx} className="badge badge-secondary">{paymentType}</span>
                );
            })}
        </>
    );
};


columns: [
                   
             {
                  Header: "Payment Type",
                  accessor: "paymentType",
                  // Cell method will provide the cell value; we pass it to render a custom component
                  Cell: ({ cell: { value } }) => <PaymentTypes values={value} />
             }
         ]

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

>Solution :

You can use ternary operators for example:

const PaymentTypes = ({ values }) => {
    // Iterate the array and create a badge-like component
    return (
        <>
            {values.map((paymentType, idx) => {
                return (
                    <span key={idx} className={paymentType==2?"badge badge-secondary":paymentType==1?"badge badge-primary":"badge badge-n"}>{paymentType}</span>
                );
            })}
        </>
    );
};
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