【问题标题】:Antd: Rangepicker issueAntd:范围选择器问题
【发布时间】:2020-02-28 08:20:08
【问题描述】:

每次我添加一个 Antd TimePicker.Rangepicker 我都会得到这个异常:

BlockquoteError:元素类型无效:需要一个字符串(对于内置组件)或一个类/函数(对于复合组件),但得到:未定义。您可能忘记从定义组件的文件中导出组件,或者您可能混淆了默认导入和命名导入。

到目前为止,这是我的组件:

// React
import React, { useState, useEffect } from 'react';
// Redux
import { useDispatch } from 'react-redux';
// Third party
import {
    Form, Input, DatePicker, Button, TimePicker
} from 'antd';

import 'antd/dist/antd.css';
import moment from 'moment';
// Site
import '../../CSS/TimeLogForm.css';
import timeLogService from '../../services/timeLogService.js';
import timeService from '../../services/timeService.js';
import * as calendarActions from '../../redux/actions/Tidsinmatning/CalendarActions';
import TimeLogCascaderComponent from './TimeLogCascader';

const TimeLogForm = (prop) => {
    // Form input  
    const [input, setInput] = useState({
        date: { value: moment(), validateStatus: "", help: "" },
        startTime: { value: null, validateStatus: "", help: "" },
        stopTime: { value: null, validateStatus: "", help: "" },
        timeReduction: { value: 0, validateStatus: "", help: "" },
        description: { value: null, validateStatus: "", help:"" },
        internalNote: { value: null, validateStatus: "", help: "" },
        isOverTime: false,
        isOverTimeAsHours: false
    });        
    // Alias    
    const dispatch = useDispatch();
    const { RangePicker } = TimePicker;            

    // =====================
    //     EVENTS
    // =====================
    function onChangeDate(e) {        
        console.log(e);
        setInput({ ...input, date: { ...input.date, value:e} });
    }
    function onChangeDescription(e) {          
        const { description } = input;
        description.value = e.target.value;
        if (description.value.length == 0) {
            description.validateStatus = "error";
            description.help = "Beskrivning saknas.";
        } else if (description.value.length >= 500) {
            description.validateStatus = "error";
            description.help = "Beskrivningen är för lång, använd max 500 tecken.";
        }
        else {
            description.validateStatus = "";            
        }
        setInput({ ...input, description });            
    }
    function onChangeInternalNote(e) {
        const { internalNote } = input;
        internalNote.value = e.target.value;        
        setInput({ ...input, internalNote });
    }
    function onChangeTime(value, timeString) {
        console.log(value,timeString);
    }
    async function onSubmit(e) {
        //e.preventDefault();
    }

    // =====================
    //     RENDER
    // =====================
    
    return (
        <div className="p-2">
            <Form onSubmit={onSubmit} size="small" labelCol={{ span: 4 }} wrapperCol={{ span: 20 }} layout="horizontal" >
                { /* TIME */}
                <Form.Item label="Klockslag">
                    <RangePicker />
                </Form.Item>
                { /* Date */}
                <Form.Item label="Datum">
                    <DatePicker value={input.date.value} onChange={onChangeDate} />
                </Form.Item>                
                { /* DESCRIPTION */}
                <Form.Item label="Beskrivning"
                    validateStatus={input.description.validateStatus}
                    help={input.description.validateStatus == "error" ? input.description.help : ''}>
                    <Input
                        placeholder="Beskrivning"
                        allowClear
                        value={input.description.value}
                        onChange={onChangeDescription} />
                </Form.Item>
                { /* NOTE */}
                <Form.Item label="Anteckning"
                    validateStatus={input.internalNote.validateStatus}
                    help={input.internalNote.validateStatus == "error" ? input.internalNote.help : ''}>
                    <Input
                        placeholder="Anteckning"
                        allowClear
                        value={input.internalNote.value}
                        onChange={onChangeInternalNote} />
                </Form.Item>
                <Form.Item className="">
                    <Button type="primary" className="m-2">Spara</Button>
                    <Button className="m-2">Ångra</Button>
                </Form.Item>
            </Form>
        </div>
    );
};

export default (TimeLogForm);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

【问题讨论】:

  • 我尝试为您的更改创建一个代码框 - codesandbox.io/s/snowy-voice-326o0。但是没有错误并且组件被正确渲染。还是你期待别的东西?
  • 我刚发现我用的是Antd versoin 3而不是4,它不支持Rangepicker!

标签: javascript reactjs antd


【解决方案1】:

我发现我用的是Antd 3.16版本,不支持Rangepicker。当我更改为第 4 版时,它起作用了。

【讨论】:

    猜你喜欢
    • 2021-02-12
    • 1970-01-01
    • 2014-11-16
    • 2011-07-16
    • 2010-09-18
    • 2020-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多