简体   繁体   English

反应日期时间; 我正在使用二合一组件,我如何分辨哪个是哪个?

[英]React-Datetime; I'm using two in one component, how do I tell which is which?

This is an odd question about react-datetime: Why doesn't the callback allow a "name" to be returned?这是一个关于 react-datetime 的奇怪问题:为什么回调不允许返回“名称”?

I'm using Daytime twice for "start" and "end" times on a calendar scheduler.我在日历调度程序上使用 Daytime 两次作为“开始”和“结束”时间。 But I want to use a single "onChange" event to update state.但我想使用单个“onChange”事件来更新 state。 The problem is: Datetime onChange only returns the time, it doesn't return a name or an ID or any way for me to identify which Datetime component is sending the onChange.问题是:Datetime onChange 只返回时间,它不返回名称或 ID 或任何方式让我识别哪个 Datetime组件正在发送 onChange。

Here is the render() code:这是渲染()代码:

<CardSubtitle>Start Date:
  <Datetime 
    value = {this.state.start}
    onChange={this.handleChange}
    className = "dateTimeModule"
  />
</CardSubtitle>

<CardSubtitle>End Date:
  <Datetime 
    value = {this.state.end}
    onChange={this.handleChange}
    className = "dateTimeModule"
  />
</CardSubtitle>

And here is the logic I want to use:这是我要使用的逻辑:

handleChange = (e) => {

  this.setState({
    bodyEdit: true,
    [e.target.name]:e.target.value
  })
};

But "e" only contains the date object.但“e”只包含日期 object。 It appears that Datetime doesn't support a "name" return?看来 Datetime 不支持“名称”返回?

I'm confused, it seems like an obvious miss.我很困惑,这似乎是一个明显的失误。 How do I differentiate between these?我如何区分这些? Do I need to write separate functions for each?我需要为每个函数编写单独的函数吗?

Thinking about doing this:考虑这样做:

handleStartChange = (e) => {
  this.setState({
    startDate: e
  })
};

handleEndChange = (e) => {
  this.setState({
    endDate: e
  })
};

But this seems like unnecessary code.但这似乎是不必要的代码。

What you can do is pass yourself an additional parameter to your function:你可以做的就是给你的 function 传递一个额外的参数:

        <CardSubtitle>Start Date:
          <Datetime 
            value = {this.state.start}
            onChange={(e) => this.handleChange(e, "start")}
            className = "dateTimeModule"
          />
        </CardSubtitle>

        <CardSubtitle>End Date:
          <Datetime 
            value = {this.state.end}
            onChange={(e) => this.handleChange(e, "end")}
            className = "dateTimeModule"
          />
        </CardSubtitle>

and reuse this parameter in your handleChange(e, name) function:并在您的 handleChange(e, name) function 中重用此参数:

  handleChange = (e, name) => {
    this.setState({
      bodyEdit: true,
      [name]:e
    })
  };

Also make sure of what is returned by the library you are using as the "e", it doesn't seem to be a classical event but rather a moment object:还要确保您用作“e”的库返回的内容,它似乎不是经典事件,而是一个时刻 object:

Callback trigger when the date changes.日期更改时的回调触发。 The callback receives the selected moment object as only parameter, if the date in the input is valid.如果输入中的日期有效,则回调接收选择的时刻 object 作为唯一参数。 If the date in the input is not valid, the callback receives the value of the input (a string).如果输入中的日期无效,则回调接收输入的值(字符串)。

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

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