Home > Mobile >  How to access the output of an array in a if-statement ReactJS
How to access the output of an array in a if-statement ReactJS

Time:07-04

I'm completely new to this, I would like to do make all buttons with values where slot.booked === 1 red and disabled.

How do i access the value the if statement outputs?

 const renderTableData = () => {
let id = 1;

return (
  <tr>
    {days.map((val) => (
      <td>
        {timeSlot.map((n, i) => {
          if (freeSlotsList.some(slot => slot.booked === 1)) {
            const activeButton = (<h1>Test</h1>)
            
          
          return (
            <button id={id  } className={activeButton}>
              {n} {activeButton} {id}
            </button>
          ) 
        } else 
            return (
                <button id={id  }>
                  {n}  {id}
                </button>
            )
          
        })}
      </td>
    ))}
  </tr>
);

};

CodePudding user response:

Create a new variable and use it in if condition

 const renderTableData = () => {
  let id = 1;

  return (
    <tr>
      {days.map((val) => (
        <td>
          {timeSlot.map((n, i) => {
            const items = freeSlotsList.some(slot => slot.booked === 1) 
            if (items) {
              const activeButton = (<h1>Test</h1>)


            return (
              <button id={id  } className={activeButton}>
                {n} {activeButton} {id}
              </button>
            ) 
          } else 
              return (
                  <button id={id  }>
                    {n}  {id}
                  </button>
              )

          })}
        </td>
      ))}
    </tr>
  );
}
  • Related