1

I'm currently learning if else statement in React,

So i want the button in the table to appear if the variable "step" is set to 1, other than that the button (and the row) wont appear. What method is the best to implement this if else?

function DeletableGroupRow({
  student,
  hasDeleteButton = false,
  onDeleteStudent,
  step,
}) {
  DeletableGroupRow.propTypes = {
    student: PropTypes.object.isRequired,
    hasDeleteButton: PropTypes.bool,
    onDeleteStudent: PropTypes.func.isRequired,
  };
  const handleDeleteClick = () => {
    onDeleteStudent(student.id);
  };

  return (
    <tr>
      <td>{student.nim}</td>
      <td>{student.name}</td>
      <td>{student.class}</td>
      <td>{student.peminatan.abbrev}</td>
      // if the variable "step" is set to 1 then this td appears
      <td>
        <button
          type="button"
          className="btn btn-default"
          onClick={handleDeleteClick}
          disabled={!hasDeleteButton}
        >
          Hapus
        </button>
      </td>
      //
    </tr>
  );
}
devserkan
  • 16,870
  • 4
  • 31
  • 47
Buffaurus
  • 11
  • 1
  • 3

4 Answers4

2

here's how to do a conditional rendering

<td>
 { step === 1 ? 
    <button type="button" 
    className="btn btn-default" 
    onClick={handleDeleteClick} 
    disabled={!hasDeleteButton}>Hapus</button> 
    : null 
 }
            
</td>
Ali Abu Hijleh
  • 492
  • 3
  • 9
1

If I understood your question correctly there is two way we can do it (Known to me).

function DeletableGroupRow({
  student,
  hasDeleteButton = false,
  onDeleteStudent,
  step,
}) {
  DeletableGroupRow.propTypes = {
    student: PropTypes.object.isRequired,
    hasDeleteButton: PropTypes.bool,
    onDeleteStudent: PropTypes.func.isRequired,
  };
  const handleDeleteClick = () => {
    onDeleteStudent(student.id);
  };

  if (step === 1) {
  return (
    <tr>
      <td>{student.nim}</td>
      <td>{student.name}</td>
      <td>{student.class}</td>
      <td>{student.peminatan.abbrev}</td>
      // if the variable "step" is set to 1 then this td appears
      <td>
        <button
          type="button"
          className="btn btn-default"
          onClick={handleDeleteClick}
          disabled={!hasDeleteButton}
        >
          Hapus
        </button>
      </td>
      //
    </tr>
  );
}
} else {
 return </>
}

OR

return (
  { step === 1 && <tr>...</tr> }
  { step === 0 && </>}
);

</> may or may not be needed, people with sound expertise in react may correct this.

Tarik1322
  • 80
  • 7
0

you can use && operator which mean if and only if is true. It is a shorthand to if-else but simpler and readable. But if you want render something else if your condition is false then use if-else.

{step === 1 && (
 <td>
    <button
      type="button"
      className="btn btn-default"
      onClick={handleDeleteClick}
      disabled={!hasDeleteButton}
    >
      Hapus
    </button>
  </td>
 )}
Y Allaban
  • 112
  • 1
  • 1
  • 8
0

you can't use if / else statement inside the return i think. You have two possibity, ternary inside rendering like this

 { 
       step === 1 &&
  <td>
          <button type="button" 
          className="btn btn-default" 
          onClick={handleDeleteClick} 
          disabled={!hasDeleteButton}>
            {'Hapus'}
          </button> 
           
 </td>
}

Or a sub component like here