英文:
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 '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}
/>
);
}
It is working fine for end time. Thanks.
答案1
得分: 0
以上的代码在使用最新版本的React时工作正常,您可以根据以下示例来简化一些代码
您可以避免
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
- Binding read more here
- 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 "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);
<!-- end snippet -->
Working codesandbox
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论