我在 React js 中使用日期选择器
https://www.npmjs.com/package/react-datepicker
但是我当前的值是空白 null
。现在我想在我关注 输入字段
时显示日期选择器。但是我收到错误/警告这是为什么?
proxyConsole.js:72 Warning: Failed prop type: Invalid prop `selected` of type `String` supplied to `Calendar`, expected instance of `Date`.
in Calendar (created by OnClickOutside(Calendar))
in OnClickOutside(Calendar) (created by DatePicker)
in div (created by InnerPopper)
in InnerPopper (created by Context.Consumer)
in Popper (created by PopperComponent)
in Manager (created by PopperComponent)
in PopperComponent (created by DatePicker)
in DatePicker (created by Main)
其实我在转换数据的时候加了一个条件
data[i].displaytext = i;
if (data[i].dataType === "DATE" && data[i].value) {
data[i].value = new Date(data[i].value);
}
obj[lastLabel] = [...obj[lastLabel], data[i]];
}
这是我的代码 https://codesandbox.io/s/rmx02zzr5o
请帮我删除这个警告
最佳答案
看起来 DOB
对象中的默认 value
是一个字符串,但是您要将其传递给的组件需要一个 Date
。好像变了
DOB: {
value: "",
type: "FIELD",
editable: true,
dataType: "DATE",
required: true,
displayName: null,
pattern: null
}
到
DOB: {
value: null, // <- specifically this line
type: "FIELD",
editable: true,
dataType: "DATE",
required: true,
displayName: null,
pattern: null
}
将至少使错误消失并且日期选择器工作,至少在它设置文本字段中的值的范围内。
关于javascript - 提供给 `selected` 的 `String` 类型的无效 Prop `Calendar`, `Date` 的预期实例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53689969/