swiper: `Cannot convert undefined or null to object` when using `Thumbs` module
Check that this is really a bug
- I confirm
Reproduction link
https://github.com/bnn1/swiper-issue
Bug description
When creating component from example in React app, app throws runtime error TypeError: Cannot convert undefined or null to object
.
Reproduction link contains next.js app but it is possible to reproduce with pure react bootstrapped with CRA
Steps to reproduce:
- Create react app
yarn create react-app
- Install
swiper
- Create component
Carousel
and add the following code
code
import React, { useState } from "react";
import { Swiper, SwiperSlide } from "swiper/react";
import { Thumbs } from "swiper";
import "swiper/css";
export const Component = () => {
// store thumbs swiper instance
const [thumbsSwiper, setThumbsSwiper] = useState(null);
return (
<main>
{/* Main Swiper -> pass thumbs swiper instance */}
<h1>hello</h1>
<Swiper modules={[Thumbs]} thumbs={{ swiper: thumbsSwiper || null }}>
<SwiperSlide>1</SwiperSlide>
<SwiperSlide>2</SwiperSlide>
<SwiperSlide>3</SwiperSlide>
<SwiperSlide>4</SwiperSlide>
<SwiperSlide>5</SwiperSlide>
</Swiper>
{/* Thumbs Swiper -> store swiper instance */}
{/* It is also required to set watchSlidesProgress prop */}
<Swiper modules={[Thumbs]} watchSlidesProgress onSwiper={setThumbsSwiper}>
{/* ... */} <SwiperSlide>1</SwiperSlide>
<SwiperSlide>2</SwiperSlide>
<SwiperSlide>3</SwiperSlide>
<SwiperSlide>4</SwiperSlide>
<SwiperSlide>5</SwiperSlide>
</Swiper>
</main>
);
};
import { Component } from './Component';
function App() {
return (
<div className="App">
<Component />
</div>
);
}
export default App;
- Run application
yarn start
- Navigate to
localhost:3000
- Open dev tools
Expected Behavior
No errors occur during runtime
Actual Behavior
Errors occur during runtime
Swiper version
8.1.0
Platform/Target and Browser Versions
Linux (arch), Chrome Version 100.0.4896.88 (Official Build) (64-bit)
Validations
- Follow our Code of Conduct
- Read the docs.
- Check that there isn’t already an issue that request the same feature to avoid creating a duplicate.
- Make sure this is a Swiper issue and not a framework-specific issue
Would you like to open a PR for this bug?
- I’m willing to open a PR
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 17
- Comments: 24
the workaround is
thumbs={{ swiper: thumbsSwiper }}
replace tothumbs={{ swiper: thumbsSwiper && !thumbsSwiper.destroyed ? thumbsSwiper : null }}
I am facing with this issue as well with Nextjs 12 + React 18
This didn’t work for my case.
From the thumbs React demo, it has
const [thumbsSwiper, setThumbsSwiper] = useState(null);
I console logged
thumbsSwiper
anddestroyed
was coming up astrue
. Well, the whole story is thatthumbsSwiper
was console loggingnull
at first when rendering & then after another render on its own, it gave me the output:Swiper { "destroyed": true }
. My guess is that the error in question came up because of that firstnull
rendering fromthumbsSwiper
.I just render my component like so:
arrOfDataObjs && <MultiSliders />
.So what worked for me was adding
thumbsSwiper
as a condition to rendering:arrOfDataObjs && thumbsSwiper && <MultiSliders />
.I’m using create-react-app with React v16.13.1 & Swiper v8.2.4
<Swiper cssMode={true} loop={true} spaceBetween={10} navigation={true} thumbs={{ swiper: thumbsSwiper && !thumbsSwiper.destroyed ? thumbsSwiper : null }} onInit={(swiper) => { setInit(swiper); }} modules={[FreeMode, Navigation, Thumbs]} className=“mySwiper2” />
thumbs={{ swiper: thumbsSwiper && !thumbsSwiper.destroyed ? thumbsSwiper : null }}
buni
where do we write this?
same problem. src is undefined. use import {Swiper} from ‘swiper/vue’. maybe something wrong in build not using esm import
The work-around
thumbs={{ swiper: thumbsSwiper && !thumbsSwiper.destroyed ? thumbsSwiper : null }}
works but why was this closed? And shouldn’t the docs be updated? Is it because of the missing demo? Maybe someone will be able to provide one? There are many participants in this thread.My solution to the problem:
Swiper 11.05
thumbs={{swiper: thumbsSwiper && !thumbsSwiper.destroyed ? thumbsSwiper : null}}
the answer of the MaksimAmelchenko is working