Trouble installing Swiper in my React project

10,410

Solution 1

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.

Solution 2

yo, if you use [email protected] then there is no 'swiper/css/swiper.css'. there is only 'swiper/swiper.scss', so if you can work with that, go for it.

in my case i downgraded to [email protected] because in that version there is still the css folder and file.

Solution 3

As of [email protected], the style imports look like this:

swiper.less - core Swiper LESS

swiper.scss - core Swiper SCSS

swiper-bundle.css - Swiper bundle CSS

More info can be found here: https://swiperjs.com/changelog#6-0-0-released-on-july-3rd-2020

Share:
10,410

Related videos on Youtube

Giulio Serra
Author by

Giulio Serra

Updated on May 26, 2022

Comments

  • Giulio Serra
    Giulio Serra almost 2 years

    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 <[email protected]>
       */
      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.