Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unpkg usage throwing react type is invalid error #981

Closed
ShyamRaj opened this issue Jan 27, 2020 · 21 comments
Closed

unpkg usage throwing react type is invalid error #981

ShyamRaj opened this issue Jan 27, 2020 · 21 comments
Labels
Build Issue related to the built files help wanted Extra attention is needed Priority: High Priority: Medium To Investigate Needs more investigation from the mantainers v7 (maintenance)

Comments

@ShyamRaj
Copy link

Describe the bug
When I try to use react-day-picker I get the follow errors in my console

Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.

To Reproduce
I can reproduce the error in the sandbox here -https://codesandbox.io/s/react-day-picker-base-vt99v

Expected behavior
Should be able to use the react-day-picker normally

@KyorCode
Copy link

window.DayPicker is actually undefined. Hence you getting that error.

May I suggest to use import to get the DayPicker.

@ShyamRaj
Copy link
Author

ShyamRaj commented Jan 27, 2020

I have been using the import method until last week, My original issue is when I started to bundle react-day-picker as es6 module,
So little background, I have a custom component library and recently switched to use rollup instead of webpack to bundle es6 modules so it can be tree shaken on the projects its being not used.

When I run the project using the component library which uses react-day-picker I get a lot more errors, Which looks like react day picker does not export things correctly for me to bundle as esm.

@KyorCode
Copy link

I'm currently using it in a rollup based component library. Is it possible to share those errors?

On the other hand, it does indeed seem that react-day-picker isn't exported correctly on the window when using it as and esm package.

@ShyamRaj
Copy link
Author

This is the error I get

Warning: React.createElement: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: object.

Check the render method of `DatePicker`.

In the render function I have a used DayPickerInput using the wrapper component from my component library. which imports import DayPickerInput from 'react-day-picker/DayPickerInput';

Here is the render function of the library wrapper component

<DayPickerInput
          classNames={{
            container: 'DatePickerInput',
            overlayWrapper: showOrHideDatePickerOverlay,
            overlay: ''
          }}
          value={this.state.date}
          onDayChange={this.handleDayChange}
          formatDate={formatDate}
          parseDate={parseDate}
          placeholder={placeholder}
          dayPickerProps={dayPickerProps}
          inputProps={{
            kind: 'date',
            label,
            labelPosition,
            disabled,
            hasFocus,
            helpText,
            material,
            muteValidation,
            name,
            regex,
            required,
            inRange: this.state.inRange,
            validityCheck,
            errormessage,
            maxLength: 10
          }}
          component={Input}
        />

In my component library rollup config, I have these plugins and commonjs with namedExports

 plugins: [
      babel(),
      resolve(),
      commonjs({
        namedExports: {
          'react-day-picker/moment': ['formatDate', 'parseDate'],
          'react-dom': ['createPortal', 'findDOMNode', 'render'],
        }
      }),

Do I have to do any namedExports for react-day-picker/DayPickerInput because I cannot find any export statement in DayPicker.js or types/index.ts under node_moduels/react-day-picker.

If possible can you share a snippet of your plugins in rollup.config.js

@ShyamRaj
Copy link
Author

I just tried to use just a simple DayPicker example from http://react-day-picker.js.org/docs/getting-started like this import DayPicker from 'react-day-picker'; inside the component library and used the wrapper component in the project and I'm still getting the same error as before.

@paulrostorp
Copy link

Getting this error as well when trying to import DayPickerInput:

import DayPickerInput from 'react-day-picker/DayPickerInput';
[...]
<DayPickerInput onDayChange={day => console.log(day)} />
Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object.

Using rollup (with typescript & commonjs plugin)...

@KyorCode
Copy link

could you add which version you are using?
https://codesandbox.io/s/react-day-picker-base-pwtp2

I tried with 7.4.0 and it seems to be working.

My commonjs looks like this:
commonjs({ include: 'node_modules/**' }),

No named exports.

Also using the import statement in your example, it just works:
https://codesandbox.io/s/react-day-picker-base-7sxpk

There is still a problem with the esm package though.

@paulrostorp
Copy link

@KyorCode using v7.4.0 as well.
Also the code sandbox you sent doesn't work...

@ShyamRaj
Copy link
Author

@KyorCode Both the sandbox code has issues and does not work.

Also tried using your commonjs configuration, I get the below error,

[!] Error: 'formatDate' is not exported by node_modules/react-day-picker/moment.js
https://rollupjs.org/guide/en/#error-name-is-not-exported-by-module
src/components/DatePicker/DatePicker.js (11:9)
 9: import { DateUtils } from 'react-day-picker';
10: import DayPickerInput from 'react-day-picker/DayPickerInput';
11: import { formatDate, parseDate } from 'react-day-picker/moment';

So after little digging removed import { formatDate, parseDate } from 'react-day-picker/moment'; and copy pasted a sample example code from the site http://react-day-picker.js.org/docs/input. This time the build was successful but I get the same error which is the original error I got using unpkg,

Warning: React.createElement: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: object.

Check the render method of `DatePicker`.
    in DatePicker (created by Hello)

FYI, in all the above attempts my component library runs fine with expected output, the issue occurs only when I use the wrapped component from my component library inside the actual project.

@KyorCode
Copy link

#919

@ShyamRaj
Copy link
Author

@KyorCode I think its similar but different in many ways, I tried v7.3.0 and i still have the same issue.

@KyorCode
Copy link

KyorCode commented Jan 30, 2020

@ShyamRaj could you post your wrapped component? Im starting to think the problem occurs there instead of the library.

@drbeat
Copy link

drbeat commented Feb 13, 2020

I have the exact same issue.
Its not an issue with the wrapped component as a simple component like

import DayPickerInput from 'react-day-picker/DayPickerInput';
const Picker = () => (<DayPickerInput />)
export default Picker;

already causes it.

my rollup config looks like this

export default {
  input: 'src/lib/index.js',
  output: [
    {
      file: pkg.main,
      format: 'cjs',
      sourcemap: true,
    },
    {
      file: pkg.module,
      format: 'es',
      sourcemap: true,
    },
  ],
  external: ['styled-components'],
  plugins: [
    external(),
    postcss({
      modules: true,
    }),
    url(),
    babel({
      exclude: 'node_modules/**',
    }),
    resolve({
      extensions: ['.js', '.jsx'],
    }),
    commonjs(),
    filesize(),
  ],
};

i also tried removing the exlusion of "node_modules" from babel and add inclusion to commonjs but didnt work either

@coreybrown89
Copy link

I am having the same issue as @drbeat . I am also using inside of a component library using rollup to package everything up.

@kelvinlouis
Copy link

Has anyone found a workaround yet? I have the same (see @coreybrown89 and @drbeat) issue concerning the bundle produced by Rollup.

@gpbl gpbl added this to the v7 milestone Apr 13, 2020
@gpbl gpbl added To Investigate Needs more investigation from the mantainers Priority: Medium help wanted Extra attention is needed labels Apr 13, 2020
@gpbl
Copy link
Owner

gpbl commented Apr 17, 2020

This has been fixed in v7.4.5 not sure about that

@gpbl gpbl closed this as completed Apr 17, 2020
@gpbl gpbl reopened this Apr 17, 2020
@gpbl gpbl added Priority: High PR Welcome Welcome to Pull Request and removed PR Welcome Welcome to Pull Request labels Apr 17, 2020
@DerekHung
Copy link

I am having the same issue, when I wrap up the DatePickerInput & produce ES module by Rollup.
Does anyone have workaround solution?

@gpbl gpbl added v7 (maintenance) Build Issue related to the built files labels Feb 10, 2021
@Yuliana7
Copy link

The issue seems to be with the export of this component, I'm not sure there's a way to trick rollup into building it into something it's not. If the author could export default like he did with <DayPicker /> - it would solve the problem, I think. Using <DayPicker /> in consumer component isn't causing any problems, commonjs plugin resolves it as a function, unlike <DayPickerInput />

in 'index.d.ts' add:

export { default } from './DayPickerInput';

in your consumer component:

import DayPickerInput from 'react-day-picker';

@drbeat
Copy link

drbeat commented May 7, 2021

This is a nice workaround
#1194 (comment)

@ivandax
Copy link

ivandax commented Jun 11, 2021

I had this issue on version 7.4.10, and I had to downgrade to react-day-picker version 7.4.8 and it worked.

@gpbl
Copy link
Owner

gpbl commented Mar 26, 2022

Closing as we've released v8: this issue belongs to a version which is not longer maintained, and it is likely fixed. In case, please open a new issue. Thanks!

@gpbl gpbl closed this as completed Mar 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Build Issue related to the built files help wanted Extra attention is needed Priority: High Priority: Medium To Investigate Needs more investigation from the mantainers v7 (maintenance)
Projects
None yet
Development

No branches or pull requests

10 participants