Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Swiper with React 18 causes TypeError: Cannot read properties of undefined (reading 'slideClass') #5613

Closed
5 of 6 tasks
cizz3007 opened this issue Apr 6, 2022 · 23 comments
Closed
5 of 6 tasks

Comments

@cizz3007
Copy link

cizz3007 commented Apr 6, 2022

Check that this is really a bug

  • I confirm

Reproduction link

none

Bug description

TypeError: Cannot read properties of undefined (reading 'slideClass')

image

Expected Behavior

No response

Actual Behavior

image

Swiper version

8.0.7

Platform/Target and Browser Versions

macOs Chrome latest

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
@cizz3007 cizz3007 added the React label Apr 6, 2022
@cizz3007
Copy link
Author

cizz3007 commented Apr 6, 2022

I Guess It was caused by React Strict Mode

@ghost
Copy link

ghost commented Apr 6, 2022

Due to React18, there was a problem that you could not specify className for SwiperSlide.

To avoid that I downgraded to React 17.

@github-actions
Copy link

Hello @cizz3007. Please provide a online reproduction by codesandbox or a minimal GitHub repository. You can fork one of our demos in codesandbox to get start. Issues labeled by missing demo will be closed if no activities in 3 days.

@Muhammad-Zeshan-Tufail
Copy link

Due to React18, there was a problem that you could not specify className for SwiperSlide.
you can use a new div between the SwiperSlide and there use your classes

@dottodot
Copy link

I'm having this issue when used with nextjs but just tried to replicate with a basic example on codepen and I'm not getting the error, so there must be something else in my setup causing it. https://codesandbox.io/s/swiper-default-react-forked-3y14np

@raphaelsaunier
Copy link

Running into this issue as well with Next.js 12.1.5 + React 18.0.0; at one point getSlideClasses gets called on a destroyed Swiper instance and this causes the exception:

image

@Muhammad-Zeshan-Tufail's workaround seems to work; if the SwiperSlide component doesn't have a className, no exception is thrown

I'll try to create codesandbox reproduction; @nolimits4web what's your preferred workflow for issues that were closed with the missing demo label? Will you reopen this issue if I post a repro here or shall I create a new issue?

@nolimits4web
Copy link
Owner

@raphaelsaunier better create a new one

@OlegStrokan
Copy link

Setting className on SwiperSlide triggers runtime error with React 18 in strict mode #5656

You are right. I just deleted React.StrictMode and it's working

@jordanlambrecht
Copy link

Upgrading to ^8.2.2 seemed to fix it for me without needing to switch strict mode to false

@DongwonTTuna
Copy link

DongwonTTuna commented Feb 2, 2023

To solve this problem, you need to update swiper to latest version.

try this command to update your swiper to latest version.

npm install swiper@latest

or

yarn add swiper@latest

I hope it can help you.

@evgeniy28
Copy link

Latest swiper don't work for me.
I fix this problem only downgrade swiper to latest 8 version:

"next": "13.1.5",
"react": "18.2.0",
"react-dom": "18.2.0",
- "swiper": "9.0.2",
+ "swiper": "8.4.7",

@TFMahabub
Copy link

Latest swiper don't work for me. I fix this problem only downgrade swiper to latest 8 version:

"next": "13.1.5",
"react": "18.2.0",
"react-dom": "18.2.0",
- "swiper": "9.0.2",
+ "swiper": "8.4.7",

how did you install "swiper": "8.4.7" with yarn?

@mamal72
Copy link

mamal72 commented Feb 5, 2023

It's still there on 9.0.2. The downgrade to 8.4.7 was the only way.

@mahadidev
Copy link

mahadidev commented Feb 5, 2023

right
wrong
If you are using Nextjs Image component inside the slide then:
use: <img src="' alt="" />(which is a simple html tag) instead < Image src="" alt="" / > (which is coming from Nextjs)
So you are using a simple img html element instead of the Image of Nextjs

@diginikkari
Copy link

@mahadidev could you elaborate more what should be replaced when using nextjs image component?

@dertuman
Copy link

dertuman commented Feb 5, 2023

@diginikkari what he means is to use the standard img html element instead of the Image from next.js. I did this and I think as of today is the best solution

@mahadidev
Copy link

@mahadidev could you elaborate more what should be replaced when using nextjs image component?

I have updated my comment. please check it out.

@tronglongphung
Copy link

@dertuman The latest version worked without using a regular img HTML element

@dertuman
Copy link

dertuman commented Feb 8, 2023

@dertuman The latest version worked without using a regular img HTML element

Okay I'll give it a shot! Ty.

@tahericode
Copy link

Latest swiper don't work for me. I fix this problem only downgrade swiper to latest 8 version:

"next": "13.1.5",
"react": "18.2.0",
"react-dom": "18.2.0",
- "swiper": "9.0.2",
+ "swiper": "8.4.7",

I also had the problem with wrapperClass, and by updating swiper to the latest version, my problem was solved

@Hachikoi-the-creator
Copy link

Latest swiper don't work for me. I fix this problem only downgrade swiper to latest 8 version:

"next": "13.1.5",
"react": "18.2.0",
"react-dom": "18.2.0",
- "swiper": "9.0.2",
+ "swiper": "8.4.7",

I really don't think removing the Image Next componet is a good approach since it'll remove it's image optimization features, downgrading swiper to 8.4.7 worked for me as well, even made me able to use the loop={true} prop with no problems!

@krystiangryczon
Copy link

krystiangryczon commented Jun 1, 2023

In my case downgrading it to 8.4.7 just caused different error of TypeError: Cannot read properties of undefined (reading 'length') :')
For me there was slightly different error of TypeError: Cannot read properties of undefined (reading 'cssMode'), and actually it was the Cookiebot interfering with this somehow, check out here

@fresonn
Copy link

fresonn commented Jun 2, 2023

Still relevant!
I don't want to disable strict mode.

onSwiper={(swiper) => {
    const el = swiper.el
    el.addEventListener('mouseenter', () => {
      swiper.autoplay.stop()
    })

    el.addEventListener('mouseleave', () => {
      swiper.autoplay.start()
    })
 }}

Error: TypeError: Cannot read properties of undefined (reading 'stop' | 'start')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests