代码之家  ›  专栏  ›  技术社区  ›  user5711656

提供给“calendar”的“string”类型的“selected”属性无效,需要“date”的实例吗?

  •  0
  • user5711656  · 技术社区  · 6 年前

    我在react js中使用日期选择器 https://www.npmjs.com/package/react-datepicker 但我现在的价值是空的 null 。现在我想在集中精力 input field 。但我收到错误/警告为什么?

    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

    请帮助我删除此警告

    1 回复  |  直到 6 年前
        1
  •  0
  •   solarshado    6 年前

    看起来是默认的 value 在你 DOB 对象是字符串,但传递给它的组件需要 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
    }
    

    将至少使错误消失和日期选择器工作,至少在它设置文本字段中的值。