picker icon indicating copy to clipboard operation
picker copied to clipboard

picker 插件使用问题

Open Arrrrray opened this issue 4 years ago • 2 comments

Arrrrray avatar Apr 13 '21 14:04 Arrrrray

当我在umi创建的react项目中,使用re-picker这个插件来添加时间选择器的时候,点击输入框,出现了以下问题:

PickerPanel.js:123 Uncaught TypeError: Cannot read property 'getNow' of undefined

devScripts.js:5931 The above error occurred in the <PickerPanel> component:

具体可以查看下面图片和视频

import styles from './index.less';
import Picker from 'rc-picker';
import 'rc-picker/assets/index.css';

export default function IndexPage() {
  return (
    <div>
      <h1 className={styles.title}>Page index</h1>
      <Picker />
    </div>
  );
}

image image image

https://user-images.githubusercontent.com/26154710/114566348-bfb7de80-9ca4-11eb-9723-6f81d6a23413.mov

Arrrrray avatar Apr 13 '21 14:04 Arrrrray

I don't know if you still have issues with it but what I discovered about this issue is that the Date library (Moment/date-fns/dayjs) is not properly imported. To solve that, just import from one of the existing ones and pass as a prop the generateConfig such as:

import Picker from 'rc-picker';
import 'rc-picker/assets/index.css';
import momentGenerateConfig from 'rc-picker/lib/generate/moment'; // here

const MyPicker = () => <Picker generateConfig={momentGenerateConfig} />

Turns out that this leads to another issue with locale, so I also needed to import that and the final code looks similar to:

import Picker from 'rc-picker';
import 'rc-picker/assets/index.css';
import momentGenerateConfig from 'rc-picker/lib/generate/moment';
import enUS from 'rc-picker/lib/locale/en_US';

const MyPicker = () => <Picker generateConfig={momentGenerateConfig} locale={enUS} />

You can also look at some examples inside the examples dir 😄

caiangums avatar Jan 28 '22 18:01 caiangums