【问题标题】:Set state after deleting table data (td) value in React在 React 中删除表数据 (td) 值后设置状态
【发布时间】:2020-04-29 15:47:39
【问题描述】:

我构建了一个List.js 组件来存储所有约会。

所有约会的列表显示在一个表格中,一个按钮允许删除约会并发送电子邮件作为确认。

根据用户类型,每行将包含一封学生/辅导员电子邮件。我想在删除一行时收到该电子邮件(<tr> 中的 <td>)并将状态设置为该电子邮件,以便我可以使用该状态作为收件人发送电子邮件。

这是组件代码(仅包括相关位 - 如果需要,将更新):

// 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);

我尝试将value={this.state.email}/value={email} 添加到&lt;td&gt;,但它不起作用。

我也想知道getAppointmentEmail是否已正确构建,在删除按钮的onClick中调用是否正确?

你能建议吗?感谢您的帮助,不胜感激!

【问题讨论】:

  • 将邮件状态分配给输入不起作用,因为您以错误的方式启动状态。您应该再次阅读 react 教程。
  • @Aaron,只是对单击特定行的按钮后如何更改状态感到困惑。
  • appointment variable的json结构是什么?
  • 获取行:stackoverflow.com/questions/55755420/…更改事件处理程序中的状态
  • 没问题,根本不想听起来刺耳:D @GRipepi 但我能给你的最好建议是尝试自己解决问题,如果遇到任何错误等,请使用 stackoverflow。边做边学通常是最好的方法。 :)

标签: javascript reactjs redux


【解决方案1】:

您可以将电子邮件联系人存储在变量中:

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

然后点击删除按钮:

<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>

【讨论】:

  • 感谢您的回复!我将在哪里声明电子邮件变量? getStudentUsersgetCounselorUsers 都以appointment 作为参数,由&lt;tr&gt; 级别生成this.props.appointments.map((appointment) =&gt; (...))
  • 可以在map函数this.props.appointments.map((appointment) =&gt; {const email = "something" return (// your code)}}中定义email变量
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-12-23
  • 2017-07-18
  • 1970-01-01
  • 1970-01-01
  • 2021-03-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多