Skip to content
Advertisement

Trouble installing Swiper in my React project

I’m trying to add Swiper to my React Web App, I’m having trouble with the regular plugin, so I decided to use this framework instead: swiper-react specifically designed for React.

Following the getting started tutorial:

Stylesheet

/*react-id-swiper requires Swiper's stylesheet. You can use CDN or use provided stylesheet files (css or scss) from react-id-swiper/lib/styles*/

Provided stylesheet files
// scss
import 'react-id-swiper/lib/styles/scss/swiper.scss';
// css
import 'react-id-swiper/lib/styles/css/swiper.css';

It says that I need to import these CSS files but when I try to import them in my Component, at runtime I’m getting:

./src/component/component/HorizontalEventList.jsx
Module not found: Can't resolve 'react-id-swiper/lib/styles/css/swiper.css' in '/Users/giulioserra/Documents/Siti Web/Hangover/hangover/src/component/component' 

without them the result on the page is the following:

SCREENSHOT

Here is the code of the page:

     import React, { Component } from "react";
    import { Link } from "react-router-dom";
    import global from "../../resource/global.json";

        import System from "../../System/System";
        import Spinner from "../component/Spinner";

        import EventMini from "../card/EventMini";

        import Swiper from 'react-id-swiper';

export default class HorizontalEventList extends Component {
  constructor() {
    super();
    this.state = {
      dataFetched: false,
      emptyMessage: "Nulla da visualizzare.",
    };
  }

  componentDidMount() {
    //here we check if there is a callback to fetch data from
    try {
      if (this.state.dataFetched) return;

      if (this.props.datasource !== undefined) {
        this.setState({ datasource: this.props.datasource, dataFetched: true });
        return;
      }

      if (this.props.dataCallback !== undefined && !this.state.dataFetched) {
        this.props.dataCallback().then((events) => {
          this.setState({ datasource: events, dataFetched: true });
        });
      }
    } catch (err) {
      console.log({ error: err });
      this.setState({ datasource: {}, dataFetched: true });
    }
  }

  /**
   * Generate a list to display the events
   * @author Giulio Serra <giulio.serra1995@gmail.com>
   */
  generateEvenList() {
    let elements = [];

    for (const eventID in this.props.datasource) {
      const event = this.props.datasource[eventID];
      elements.push(
        <li
          key={eventID}
          style={{
            marginLeft: "30px",
            marginRight: "30px",
            marginBottom: "30px",
          }}
        >
          <EventMini event={{ [eventID]: event }} />
        </li>
      );
    }

    return (
      <div>
        <ul
          className="list-group list-group-horizontal"
          style={{ listStyle: "none", overflowX: "auto" }}
        >
          {elements}
        </ul>
      </div>
    );
  }

  render() {

    const params = {
      slidesPerView: 3,
      spaceBetween: 30,
      pagination: {
        el: '.swiper-pagination',
        clickable: true,
      }
    }

    return (
      <Swiper {...params}>
        <div>Slide #1</div>
        <div>Slide #2</div>
        <div>Slide #3</div>
        <div>Slide #4</div>
        <div>Slide #5</div>
      </Swiper>
    )
}

What am I doing wrong? Thanks.

Advertisement

Answer

It says in the Styling section of readme.

For version <=2.3.2

You can import direct from react-id-swiper/lib/styles/ (supporting css, scss)

css

import 'react-id-swiper/lib/styles/css/swiper.css'

scss

import 'react-id-swiper/lib/styles/scss/swiper.scss'

For version >=3.0.0

You should import directly from Swiper packages which supports css, scss and less

css

import 'swiper/css/swiper.css'

scss

import 'swiper/swiper.scss'

less

import 'swiper/swiper.less'

You should try importing for v3.0.0 because that is the current version on npm.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement