`react-slick` - type is invalid error

555 views Asked by At

I am getting a Uncaught (in promise) Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object. error when tried to require 'react-slick` with webpack.

const React = require("react");
const Slider = require("react-slick");

function MediaCarousel(props) {
  const settings = {
      dots: true,
      infinite: true,
      speed: 500,
      slidesToShow: 1,
      slidesToScroll: 1
    };
  return !props.stories ? null : <Slider>
      <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>
}
exports.MediaCarousel = MediaCarousel;
3

There are 3 answers

0
RVCoder On
Two things you will need to add in your code 
1. <Slider {...settings}>
2. module.exports = SimpleSlider;
below code is working for me, please find if it could help you:


import React from 'react';
import {render} from 'react-dom';
import Slider from 'react-slick';
require('./carousel.scss');

class SimpleSlider extends React.Component {
 render() {
      var settings = {
      dots: true,
      autoplay: true,
      arrows: true
    };
    return (
      <Slider {...settings}>
            <div><img src='../../src/assets/1.jpg' /></div>
          <div><img src='../../src/assets/2.jpg' /></div>
          <div><img src='../../src/assets/3.jpg' /></div>
          <div><img src='../../src/assets/4.jpg' /></div>
        </Slider>
    );
}
}

module.exports = SimpleSlider;
0
Asim K T On

It was fixed when tried with

const Slider = require("react-slick").default;

I suppose react-slick is not correctly exporting the module or my loader has some issue.

0
Alessandro Messori On

try passing the settings to the Slider components :

<Slider {...settings} >