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

Element type is invalid: expected a string (for built-in components) or a class/function... #2206

Open
PremaYT opened this issue Dec 23, 2022 · 7 comments

Comments

@PremaYT
Copy link

PremaYT commented Dec 23, 2022

I am using react-slick as a dependency in one of my custom react package. Slider works fine in the component whereas it throws below error when we publish the component as a package and use it in another component.

Error :

Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object. Check the render method 'SimpleSlider'.

import Slider from "react-slick";

const SimpleSlider = () => {
    const settings = {
      dots: true,
      infinite: true,
      speed: 500,
      slidesToShow: 1,
      slidesToScroll: 1
    }
    return (
      <div>
        <Slider {...settings}>
          <div>
            <h3>1</h3>
          </div>
          <div>
            <h3>2</h3>
          </div>
          <div>
            <h3>3</h3>
          </div>
          <div>
            <h3>4</h3>
          </div>
          <div>
            <h3>5</h3>
          </div>
          <div>
            <h3>6</h3>
          </div>
        </Slider>
      </div>
    );
}

export default SimpleSlider;
@fredericrous
Copy link

I can reproduce this exact issue.
the people there too => #2118

fredericrous added a commit to fredericrous/react-slick that referenced this issue Jul 15, 2023
allow react-slick to be bundled into a library
@jordisantamaria
Copy link

Could avoid this error by using the component like this:

import Slider from "react-slick";

<Slider.default>
</Slider.default>

@bluesbroz
Copy link

Just had the same problem.
Using vite react-ts.
Running in dev mode and building was fine, but when i tried to run ssr i was getting that error.

The imported object was { default: Slider } instead of just Slider even if i tried to import like this import { default as Slider } from 'react-slick'.

Fixed with this:

// @ts-ignore
const SliderComponent = typeof window === 'undefined' ? Slider.default : Slider;

Maybe this is just an issue of my implementation of ssr.

@MoSattler
Copy link

MoSattler commented Sep 12, 2023

Still an issue - tried the .default method, but it messes up the styling.

@naghtigal
Copy link

// @ts-ignore
const SliderComponent = typeof window === 'undefined' ? Slider.default : Slider;

Thanks, it did a trick.

@paolozanotti-f2
Copy link

In our case this solution #2206 (comment) did not work. We solved it this way:

import Slider from 'react-slick'

// @ts-ignore
const SliderComponent = !!Slider.default ? Slider.default : Slider

<SliderComponent {...settings}>
...
</SliderComponent>

@Alevale
Copy link

Alevale commented Mar 18, 2024

Still an issue, at least for us on the version 0.29.0

The fix from @paolozanotti-f2 #2206 (comment) worked though, so thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants