javascript - 提供给 `selected` 的 `String` 类型的无效 Prop `Calendar`, `Date` 的预期实例?

标签 javascript reactjs redux datepicker

我在 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/

相关文章:

javascript - Angular : Convert curl to Angular $http POST request

reactjs - 使用 .htaccess 将除一个路由之外的所有路由转发到 react-router v4 的主页

reactjs - 为什么我的 reducer 返回 undefined,React TypeScript

node.js - WordPress 和 React - 前端框架内部服务器错误

javascript - 在 Javascript 对象中查找和替换键

javascript - 在 React/Redux 中插入对象时如何避免 'two children with the same key' 警告

reactjs - Redux 未在组件中同步更新

javascript - 初学者 Javascript 计时器

javascript - 关闭 jQuery Overlay 并停止播放视频

javascript - 如何在 jQuery/javascript 中添加多个监听器?