Antd Carousel transferring from class component into functional

Viewed 57

I have following code

   class Gallery extends Component {
      state = {
        media: null,
        nav: null
      };

     componentDidMount = () => {
       this.setState({
        media: this.media,
        nav: this.nav
       });
    };

     render() {
      return (
       <>
        <Carousel
          asNavFor={this.state.nav}
          touchMove={true}
          dots={false}
          ref={(carousel) => {
            this.media = carousel;
          }}
        >
          <div>
            <h3>1</h3>
          </div>
          <div>
            <h3>2</h3>
          </div>
          <div>
            <h3>3</h3>
          </div>
        </Carousel>
        <Carousel
          slidesToShow={2}
          asNavFor={this.state.media}
          ref={(carousel) => (this.nav = carousel)}
        >
          <div>
            <h3>1</h3>
          </div>
          <div>
            <h3>2</h3>
          </div>
          <div>
            <h3>3</h3>
          </div>
        </Carousel>
      </>
     );
    }
   } 

It was written based on class component, please help me to transfer it into functional component

I tried like this but it's not working correctly. When we changing thumbnail gallery the main gallery didn't changed. If I am changing thumbnail or with buttons from 1 to 2 the main slide should also change from 1 to 2. But it not changing

    const Gallery = () => {
      const [media, setMedia] = useState(null);
      const [nav, setNav] = useState(null);
      const carousel = useRef(null);

      const handleNext = () => {
        carousel.current.next();
      };

      const handlePrev = () => carousel.current.prev();

     useEffect(() => {
       setMedia(null);
       setNav(null);
     }, [media, nav]);
     return (
       <>
         <Carousel asNavFor={nav} touchMove={false} dots={false} ref= 
     {carousel}>
        <div>
          <h3>1</h3>
        </div>
        <div>
          <h3>2</h3>
        </div>
        <div>
          <h3>3</h3>
        </div>
      </Carousel>

      <Button onClick={handlePrev}> prev </Button>
      <Button onClick={handleNext}> next </Button>
      <Carousel
        slidesToShow={2}
        asNavFor={media}
        ref={carousel}
      >
        <div>
          <h3>1</h3>
        </div>
        <div>
          <h3>2</h3>
        </div>
        <div>
          <h3>3</h3>
        </div>
       </Carousel>
      </>
     );
    };

Please help me to resolve that problem, thanks :)

1 Answers

I have updated the on this link , or also you can see the below code

import React, { Component, useState } from "react";
import ReactDOM from "react-dom";
import "antd/dist/antd.css";
import "./index.css";
import { Carousel } from "antd";

const Gallery = () => {
  const mediaRef = React.useRef(null);
  const navRef = React.useRef(null);

  const onChange = (currentSlide) => {
    console.log(currentSlide);
    mediaRef.current.goTo(currentSlide, false);
  };

  return (
    <div>
      <Carousel
        asNavFor={navRef.current}
        touchMove={false}
        dots={false}
        ref={mediaRef}
      >
        <div>
          <h3>1</h3>
        </div>
        <div>
          <h3>2</h3>
        </div>
        <div>
          <h3>3</h3>
        </div>
        <div>
          <h3>4</h3>
        </div>
      </Carousel>
      <Carousel
        slidesToShow={3}
        centerMode={true}
        asNavFor={mediaRef.current}
        draggable={true}
        ref={(carousel) => (navRef.current = carousel)}
        afterChange={onChange}
        swipeToSlide={true}
        touchThreshold={50}
        focusOnSelect={true}
      >
        <div>
          <h3>1</h3>
        </div>
        <div>
          <h3>2</h3>
        </div>
        <div>
          <h3>3</h3>
        </div>
        <div>
          <h3>4</h3>
        </div>
      </Carousel>
    </div>
  );
};

ReactDOM.render(<Gallery />, document.getElementById("container"));
Related