Comments (9)
Hi there everyone I got a quick solution for this just in case anyone is having the same problem...
import React from "react";
import Swiper from "react-id-swiper";
const Slider = () => {
const [swiper, setSwiper] = React.useState(null);
let goNext = () => swiper.slideNext();
let goPrev = () => swiper.slidePrev();
React.useEffect( () => {
var mySwiper = document.querySelector(".swiper-container").swiper;
setSwiper(mySwiper);
}, [])
return (
<Swiper {...params} />
<button
className="swiper-nav-button swiper-button-prev"
onClick={goPrev}
/>
<button
className="swiper-nav-button swiper-button-next"
onClick={goNext}
/>
)
}
from react-id-swiper.
Same here. I just get TypeError: Cannot read property 'slideNext' of null
I tried the example in the readme too.
from react-id-swiper.
@kidjp85 When you get a chance, the documentation on react-id-swiper for "Example with manipulating swiper from outside swiper component" needs a tweak.
@mfundo Try the following code.
import React from 'react';
import Swiper from 'react-id-swiper';
export default class Example extends React.Component {
constructor(props) {
super(props)
this.goNext = this.goNext.bind(this)
this.goPrev = this.goPrev.bind(this)
this.swiper = null
}
goNext() {
if (this.swiper) this.swiper.slideNext()
}
goPrev() {
if (this.swiper) this.swiper.slidePrev()
}
render() {
const params = {
pagination: {
el: '.swiper-pagination',
clickable: true
},
runCallbacksOnInit: true,
onInit: (swiper) => {
this.swiper = swiper
}
}
return (
<div>
<Swiper {...params} ref={node => { if (node) this.swiper = node.swiper }}>
<div>Slide 1</div>
<div>Slide 2</div>
<div>Slide 3</div>
<div>Slide 4</div>
<div>Slide 5</div>
</Swiper>
<button onClick={this.goNext}>Next</button>
<button onClick={this.goPrev}>Prev</button>
</div>
)
}
}
from react-id-swiper.
Above code no longer seems to work (potentially due to a change in the upstream library).
from react-id-swiper.
If you are using pure react you'd better avoid querySelector
, here is just one more ref-variant from 2022
import React, { useState } from 'react'
import { Swiper, SwiperSlide } from 'swiper/react'
export const TestSlider = () => {
const [swiperRef, setSwiperRef] = useState(null);
return (
<div>
<button onClick={() => swiperRef.slidePrev()}>←</button>
<button onClick={() => swiperRef.slideNext()} >→</button>
<Swiper onSwiper = {setSwiperRef}>
<SwiperSlide>1</SwiperSlide>
<SwiperSlide>2</SwiperSlide>
<SwiperSlide>3</SwiperSlide>
<SwiperSlide>4</SwiperSlide>
</Swiper>
</div>
)
}
from react-id-swiper.
@kidjp85 do you have an idea how to solve it ?
from react-id-swiper.
Hi @Hamatek
I think we have solution for this. If you wanna manipulate the swiper from outside of it, you can do like that
import React from 'react';
import Swiper from 'react-id-swiper';
export default class Example extends React.Component {
constructor(props) {
super(props)
this.goNext = this.goNext.bind(this)
this.goPrev = this.goPrev.bind(this)
this.swiper = null
}
goNext() {
this.swiper.slideNext()
}
goPrev() {
this.swiper.slidePrev()
}
render() {
const params = {
pagination: '.swiper-pagination',
paginationClickable: true,
nextButton: '.swiper-button-next',
prevButton: '.swiper-button-prev',
spaceBetween: 30,
runCallbacksOnInit: true,
onInit: (swiper) => {
this.swiper = swiper
}
}
return(
<div>
<Swiper {...params} />
<button onClick={this.goNext}>Next</button>
<button onClick={this.goPrev}>Prev</button>
</div>
)
}
}
By using runCallbacksOnInit, onInit you can easily manipulate it from outside :)
from react-id-swiper.
@kidjp85 Do we have a work around for this issue? Can't seem to get the instance of Swiper. Even tried how you did it on using the ref still doesn't work.
Using ref I have gone as far as in the componentDidMount cycle do this:
this.swiper = this.swiperElement.current.swiper
I can see the swiper instance here and all its prototype methods but can't call them, get the same error as @abdullahseba.
from react-id-swiper.
@shiftedpixel Thanks mate...seems like the syntax of ref -> node is what I was tripping me up
from react-id-swiper.
Related Issues (20)
- keyboard not working properly on Safari
- Question: Controlled swiper HOT 3
- How to get the Current Index of the Slider ? HOT 6
- LazyLoading not working
- breakpoints not working HOT 1
- import swiper not working HOT 9
- No rtl support HOT 3
- How to get/create instance after destroyed swiper? HOT 1
- react router reload page after loop image
- Vertical Mode is not working properly HOT 1
- The examples codesandboxes are very confusing HOT 2
- Module not found: Can't resolve 'react-id-swiper/src' in 'C:\Users\cheta\React-applications\landing-page\src\components'
- How to make autoplay this swiper
- Module not found: Error: Package path ./css/swiper.css is not exported from package C:\Users\Lenovo\Documents\programacion\portfolio\portfolio\node_modules\swiper (see exports field in C:\Users\Lenovo\Documents\programacion\portfolio\portfolio\node_modules\swiper\package.json) HOT 2
- Two-way Control not working
- How to use pagination arrows outside the swiper wrapper?
- How can I create Infinite Slider ?
- slidesOffsetAfter 不生效
- Error in Next 13 / App
- Getting Error while using with next v13
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react-id-swiper.