React-Time-Range只显示默认的开始时间。

huangapple go评论73阅读模式
英文:

React-Time-Range Only Shows Default Start Time

问题

我使用 React-Time-Range 组件创建了一个表单,用于记录宴会厅预订情况。但出现了一个问题,始终默认记录的开始时间是我在状态中列出的 moment() 值。

您能否看一下我的代码,帮助确定为什么即使用户选择了不同的值,它也不会更改?

组件代码:

import moment from 'moment';
import TimeRange from 'react-time-range';

class Navbar extends Component {
 constructor(props){
 super(props)
 this.state = {
  startTime: moment(),
  endTime: moment()
 }
 }

returnFunctionStart = event => {
    this.setState({ startTime: event.startTime })
    };

  returnFunctionEnd = event => {
    this.setState({ endTime: event.endTime })
    };

render () {
return (
    <TimeRange
        onStartTimeChange={this.returnFunctionStart.bind(this)}
        onEndTimeChange={this.returnFunctionEnd}
        startMoment={this.state.startTime}
        endMoment={this.state.endTime}
    />
);
}
}

对于结束时间,它运行正常。感谢。

如果您有其他问题或需要进一步的帮助,请随时告诉我。

英文:

I've created a form using the React-Time-Range component for taking down banquet room reservations. For some reason, the start time being recorded always defaults to the moment() value I have listed in state.

Can you take a look at my code below to help determine why it does not change, even when a user selects a different value?

The component:

import moment from &#39;moment&#39;;
import TimeRange from &#39;react-time-range&#39;;

class Navbar extends Component {
 constructor(props){
 super(props)
 this.state = {
  startTime: moment(),
  endTime: moment()
}
}

returnFunctionStart = event =&gt; {
    this.setState({ startTime: event.startTime })
    };

  returnFunctionEnd = event =&gt; {
    this.setState({ endTime: event.endTime })
    };

render () {
return (
    &lt;TimeRange
        onStartTimeChange={this.returnFunctionStart.bind(this)}
        onEndTimeChange={this.returnFunctionEnd}
        startMoment={this.state.startTime}
        endMoment={this.state.endTime}
    /&gt;
);
}

It is working fine for end time. Thanks.

答案1

得分: 0

以上的代码在使用最新版本的React时工作正常,您可以根据以下示例来简化一些代码

您可以避免

  1. 绑定 在此处阅读更多
  2. 构造函数,因为Babel会处理它 在此处阅读更多
import React, { Component } from "react";
import ReactDOM from "react-dom";
import "./styles.css";
import moment from "moment";
import TimeRange from "react-time-range";

class Navbar extends Component {
  state = {
    startTime: moment(),
    endTime: moment()
  };

  returnFunctionStart = event => {
    this.setState({
      startTime: event.startTime
    });
  };

  returnFunctionEnd = event => {
    this.setState({
      endTime: event.endTime
    });
  };

  render() {
    return (
      <TimeRange
        onStartTimeChange={this.returnFunctionStart}
        onEndTimeChange={this.returnFunctionEnd}
        startMoment={this.state.startTime}
        endMoment={this.state.endTime}
      />
    );
  }
}

const rootElement = document.getElementById("root");
ReactDOM.render(<Navbar />, rootElement);

工作示例:codesandbox

英文:

The above code works fine, if you are using latest version of react you can clean up some code as shown below

You can avoid

  1. Binding read more here
  2. Constructor, since babel takes care of it read more here

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

import React, {
  Component
} from &quot;react&quot;;
import ReactDOM from &quot;react-dom&quot;;

import &quot;./styles.css&quot;;

import moment from &quot;moment&quot;;
import TimeRange from &quot;react-time-range&quot;;

class Navbar extends Component {
  state = {
    startTime: moment(),
    endTime: moment()
  };

  returnFunctionStart = event =&gt; {
    this.setState({
      startTime: event.startTime
    });
  };

  returnFunctionEnd = event =&gt; {
    this.setState({
      endTime: event.endTime
    });
  };

  render() {
    return ( &lt;
      TimeRange onStartTimeChange = {
        this.returnFunctionStart
      }
      onEndTimeChange = {
        this.returnFunctionEnd
      }
      startMoment = {
        this.state.startTime
      }
      endMoment = {
        this.state.endTime
      }
      /&gt;
    );
  }
}

const rootElement = document.getElementById(&quot;root&quot;);
ReactDOM.render( &lt; Navbar / &gt; , rootElement);

<!-- end snippet -->

Working codesandbox

huangapple
  • 本文由 发表于 2020年1月3日 21:37:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/59579577.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定