2016-12-29 74 views
0

我正在使用react-input-calendar。我想获取日期onChange事件的值,然后将其传递给新页面。 我该怎么做?如何获得价值onCahnge事件?

守则如下:

constructor() { 
    super(); 
    this.state = {value: new Date(),inputdate:''}; 
    this.handleChange = this.handleChange.bind(this); 
} 

handleChange(newDate, event) { 
    console.log(newDate); 
    this.setState({inputdate:newDate}); 
} 

<Calendar format='dddd DD MMM' closeOnSelect={true} date={this.state.value} 
ref="inputdate" inputName="inputdate" computableFormat='MM-DD-YYYY' onChange= 
{this.handleChange.bind(this,"inputdate")} /> 

我怎样才能在handleChange价值?

+0

你是什么意思setState不工作?你真的在某个地方使用它吗?如果你在'bind'的末尾摆脱了“inputdate”,那么当你改变日期时它应该会通过。如果这有正确的值,那么你可以在渲染中执行'

{this.state.inputdate}
'。 也从构造函数中删除'inputdate:'''。这不是必需的。你需要自己调试这个代码,并通过它步进 –

回答

1

props.onChange, Function

default: null

Set a function that will be triggered whenever there is a change in the selected date. It will return the date in the props.computableFormat format.

按照文档应该提供这样的:

constructor(props) { 
    super(props); 

    this.handleChange = this.handleChange.bind(this); 
} 
handleChange(newDate, event) { 
    //newDate should be passed here 
    console.log(newDate); 
} 

<Calendar format='dddd DD MMM' closeOnSelect={true} date={this.state.value0} 
ref="inputdate" inputName="inputdate" computableFormat='MM-DD-YYYY' onChange= 
{this.handleChange} /> 

检查库的src代码,并在传递的对象的属性它看起来像库调用的onChange。道具并通过newDate

if (this.props.onChange) { 
    this.props.onChange(computableDate) 
} 
+0

其投掷错误'未捕获TypeError:无法读取未定义的属性'值' –

+0

@TriyugiNarayanMani现在尝试 –

+0

@TriyugiNarayanMani号你不需要那样。这是库中的src代码。它调用我们通过newdate传入的'onChange'函数。看看我编辑的'handleChange'代码 –