简体   繁体   English

在 React 中删除表数据 (td) 值后设置 state

[英]Set state after deleting table data (td) value in React

I have built a List.js component that stores all the appointments.我已经构建了一个List.js组件来存储所有约会。

A list of all the appointments is displayed in a table and a button allows to delete an appointment and send an email as confirmation.所有约会的列表显示在表格中,一个按钮允许删除约会并发送 email 作为确认。

Each row will contain a student/counsellor email according to the user type.根据用户类型,每行将包含一个学生/辅导员 email。 I would like to get that email when I delete a row (a <td> in <tr> ) and set the state to that email so I can send an email using the state as the recipient. I would like to get that email when I delete a row (a <td> in <tr> ) and set the state to that email so I can send an email using the state as the recipient.

This is the component code (including just relevant bits - will update if needed):这是组件代码(仅包括相关位 - 如果需要,将更新):

// imports

export class Appointments extends Component {
  state = {
    email: "",
  };

  static propTypes = {
    // propsTypes...
  };

  componentDidMount() {
    // calling some async func
  }

  // other func

  getAppointmentEmail = (e) => {
    e.preventDefault();

    this.setState({ email: e.target.value });
    console.log(e.target.value);
  };

  deleteAppointmentEmail = (appointment) => {
    const { user } = this.props.auth;
    const message = "...";
    const email = {
      message,
      sender: "example@gmail.com",
      recipient: this.state.email,
    };
    console.log(email);
    // this.props.sendEmail(email);
  };

  render() {
    const { isCounselor } = this.props.clients;

    const studentHeader = <th>Student Contact</th>;
    const counselorHeader = <th>Counselor Contact</th>;

    return (
      <Fragment>
        <h4 className="text-center mt-3">Appointments</h4>
        <br />
        <table className="table table-striped mt-3">
          <thead>
            <tr>
              <th>Title</th>
              <th>Date</th>
              <th>Time</th>
              <th>Location</th>
              {isCounselor ? studentHeader : counselorHeader}
              <th />
            </tr>
          </thead>
          <tbody>
            {this.props.appointments.map((appointment) => (
              <tr key={appointment.id}>
                <td>{appointment.title}</td>
                <td>{appointment.date}</td>
                <td>{`${appointment.start_time.slice(0, 5)} -
                ${appointment.end_time.slice(0, 5)}`}</td>
                <td>{appointment.location}</td>
                <td name="email" value={this.state.email}>
                  {isCounselor
                    ? this.getStudentContact(appointment.student)
                    : this.getCounselorContact(appointment.counselor)}
                </td>
                <td>
                  <button
                    onClick={() => {
                      this.getAppointmentEmail;
                      this.props.deleteAppointment(appointment.id);
                      this.deleteAppointmentEmail(appointment);
                    }}
                    className="btn btn-danger"
                  >
                    {""}
                    Delete
                  </button>
                </td>
              </tr>
            ))}
          </tbody>
        </table>
      </Fragment>
    );
  }
}

const mapStateToProps = (state) => ({
  // ...
});

export default connect(mapStateToProps, { // ... })(Appointments);

I have tried to add value={this.state.email} / value={email} to the <td> but it is not working.我试图将value={this.state.email} / value={email}添加到<td>但它不起作用。

I am also wondering whether getAppointmentEmail has been built correctly and if calling it in the onClick of the delete button is correct?我还想知道getAppointmentEmail是否已正确构建以及在删除按钮的onClick中调用它是否正确?

Could you please advise?您能否提一些建议? Thank you for your help, it is appreciated!感谢您的帮助,不胜感激!

You can store the email contact in a variable:您可以将 email 联系人存储在变量中:

this.props.appointments.map(appointment => {
     const email = isCounselor ? this.getStudentContact(appointment.student) : this.getCounselorContact(appointment.counselor);
     return (
            <tr key={appointment.id}>
               //code inside row ...
            </tr>
      );
  })

and then on Delete button:然后在删除按钮上:

<button
  onClick={() => {
    this.getAppointmentEmail(email); // pass email inside the function and then set it in state
    this.props.deleteAppointment(appointment.id);
    this.deleteAppointmentEmail(appointment);
  }}
  className="btn btn-danger"
>
  {""}
  Delete
</button>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM