'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:
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.
Solution 1:[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
Swiperpackages which supports css, scss and lesscss
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:[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:[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
Solution 4:[4]
Try downgrading your SwipeJS to version 6.8.4 using:
npm: npm install [email protected]
yarn: yarn add [email protected]
This worked for me :)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Nicii A |
| Solution 3 | Kevin Ge |
| Solution 4 | Ale Sanchez |

