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>
  );
}
2

4 Answers 4

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>
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! is the ' : null' in the code the else case?
yes, this is called a ternary operator, which acts like if / else
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.

2 Comments

tried both your solution and can confirm both works!
Glad to know that. Mark it as answered if you feel it helped so others can benefit from this as well. @Buffaurus
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

1 Comment

Yes you can. check out the first comment.
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>
 )}

1 Comment

thanks! this one (if and only if condition) is new to me and i think this is the more elegant solution

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.