Reactjs 显示在反应日期时间选择器中选择的日期
我想在我的UI上显示用户从日期时间选择器中选择的日期和时间。我的代码如下-Reactjs 显示在反应日期时间选择器中选择的日期,reactjs,datetime,Reactjs,Datetime,我想在我的UI上显示用户从日期时间选择器中选择的日期和时间。我的代码如下- import React from 'react'; import '../App.css'; import Datetime from 'react-datetime'; class Datetimepicker extends React.Component { constructor(props) { super(props); this.state = { moveaside:
import React from 'react';
import '../App.css';
import Datetime from 'react-datetime';
class Datetimepicker extends React.Component {
constructor(props) {
super(props);
this.state = {
moveaside: false,
feed_items: [],
newText: new Date(),
};
this.updateState = this.updateState.bind(this);
this.showValuee = this.showValuee.bind(this);
}
updateState(e) {
const text = e.value;
console.log("text", text);
this.setState({ newText: text });
this.showValuee();
}
showValuee() {
console.log(this.state.newText);
}
render() {
console.log(this.props.value);
return (
<Datetime className={this.props.show ? 'rdt' : 'hide'} onChange={this.updateState} defaultValue={this.state.newText} />
)
}
}
export default Datetimepicker;
从“React”导入React;
导入“../App.css”;
从“反应日期时间”导入日期时间;
类Datetimepicker扩展React.Component{
建造师(道具){
超级(道具);
此.state={
旁白:错,
feed_项目:[],
新文本:新日期(),
};
this.updateState=this.updateState.bind(this);
this.showValuee=this.showValuee.bind(this);
}
房地产(东){
常量文本=e.value;
控制台日志(“文本”,文本);
this.setState({newText:text});
这是showValuee();
}
showValuee(){
console.log(this.state.newText);
}
render(){
console.log(this.props.value);
返回(
)
}
}
导出默认日期时间选择器;
“text”显示未定义的值。我正在父组件中导入此“Datetimepicker”组件,并且我使用的Datetime picker是此-()您使用的react日期时间选择器的文档以编程方式使用了
onChange
函数,并且不会将更改事件
转发给用户。相反,onChange
中的参数是时刻日期对象本身
类应用程序扩展了React.Component{
建造师(道具){
超级(道具);
此.state={
日期时间:“”
}
this.updateState=this.updateState.bind(this);
}
房地产(日期){
//此函数为您提供所选日期的时刻对象。
控制台日志(日期);
}
render(){
返回(
);
}
}
render(,document.getElementById(“根”))代码>
在react中,setState
是异步的。如果要记录该值,则必须使用setState的回调。