我想创建一个组件,其中包含 Datepicker和 ChoiceGroup使用 ReactJs 和 Office365。在第一次渲染时,日期选择器必须被禁用,当我们在 ChoiceGroup 中选择自定义单选按钮时,它必须被启用。代码片段:
<div>
<ChoiceGroup
defaultSelectedKey='all'
onChange={ this._onChange }
label='Date'
options={ [
{
key: 'all',
text: 'All'
},
{
key: 'thisWeek',
text: 'This week',
},
{
key: 'lastWeek',
text: 'Last week',
},
{
key: 'thisMonth',
text: 'This month',
},
{
key: 'custom',
text: 'Custom',
}
] }
/>
<DatePicker
label='label'
isRequired={ false }
disabled={ pickerDisabled }
strings={ strings }
allowTextInput={ false }
value={ value }
/>
</div>
picker 已disabled
参数,我想在 ChoiceGrou 方法中更改它:
onChange = (ev, option) => {
console.dir(option);
if(option.key === 'custom') {
this.setState({
pickerDisabled: false
})
} else {
this.setState({
pickerDisabled: true
})
}
}
此方法有效并更改参数,但日期选择器组件的禁用不会改变。它保持不变,当我单击任何单选按钮时,我在控制台中收到错误:
Exception in DatePicker.componentWillReceiveProps(): TypeError: date1.getFullYear is not a function
此错误的原因可能是什么?
最佳答案
看来您正在将无效的值
传递给DatePicker
组件,我的猜测(没有更多上下文/信息)是值
默认情况下为 null
/undefined
,DatePicker
默认为当前日期,但 value
由单选按钮,它不是一个有效的值(根据错误,看起来 DatePicker
需要一个 Date
对象(它正在尝试调用 getFullYear()
上),但 value
是一个字符串)。
检查 DatePicker
组件的文档,了解其 value
属性的预期内容并进行相应更新。
关于javascript - DatePicker 禁用错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45865763/