Code Monkey home page Code Monkey logo

Comments (9)

alexandprivate avatar alexandprivate commented on August 15, 2024 11

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.

abdullahseba avatar abdullahseba commented on August 15, 2024 6

Same here. I just get TypeError: Cannot read property 'slideNext' of null
I tried the example in the readme too.

from react-id-swiper.

shiftedpixel avatar shiftedpixel commented on August 15, 2024 3

@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.

jh3141 avatar jh3141 commented on August 15, 2024 2

Above code no longer seems to work (potentially due to a change in the upstream library).

from react-id-swiper.

chakzefir avatar chakzefir commented on August 15, 2024 1

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.

Hamatek avatar Hamatek commented on August 15, 2024

@kidjp85 do you have an idea how to solve it ?

from react-id-swiper.

kidjp85 avatar kidjp85 commented on August 15, 2024

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.

mfundo avatar mfundo commented on August 15, 2024

@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.

mfundo avatar mfundo commented on August 15, 2024

@shiftedpixel Thanks mate...seems like the syntax of ref -> node is what I was tripping me up

from react-id-swiper.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.