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

How to call the methods using react hooks. #76

Open
Shofol opened this issue Jul 22, 2020 · 3 comments
Open

How to call the methods using react hooks. #76

Shofol opened this issue Jul 22, 2020 · 3 comments

Comments

@Shofol
Copy link

Shofol commented Jul 22, 2020

What is the process to call the methods (to, next, etc.) in react hooks? Please update the README.md or give an answer here.

@jmdmacapagal
Copy link

+1 on this I cant figure it out

@diegohennrich
Copy link

What is the process to call the methods (to, next, etc.) in react hooks? Please update the README.md or give an answer here.

Hi There!

We can call methods using the hook useRef, like this:

import React, { FC, useRef, useCallback } from 'react';

import ReactOwlCarousel from 'react-owl-carousel';

const MyCarousel: FC = () => {
  const banners = [
    'https://i0.wp.com/www.lifecaretechnology.com/wp-content/uploads/2018/12/default-banner.jpg',
    'https://docs.aws.amazon.com/pt_br/quickstart/latest/linux-bastion/images/default-linux-bastion-banner.png',
  ];
  const RefCarousel = useRef<ReactOwlCarousel>(null);

  const handleNav = useCallback(
    (action: 'prev' | 'next') => {
      if (!RefCarousel.current) return;
      // 0.2 is the speed
      if (action === 'prev') {
        RefCarousel.current?.prev(0.2);
      } else {
        RefCarousel.current?.next(0.2);
      }
    },
    [RefCarousel]
  );

  return (
    <div>
      <ReactOwlCarousel
        key={`my_carousel`}
        nav={false}
        items={1}
        ref={RefCarousel}
        children={banners.map((i: string) => (
          <img key={i} src={i} />
        ))}
      />

      <div>
        <img
          style={{ width: 30, marginRight: 20 }}
          onClick={() => handleNav('prev')}
          src="https://upload.wikimedia.org/wikipedia/commons/thumb/1/12/Arrow_left.svg/1280px-Arrow_left.svg.png"
        />
        <img
          style={{ width: 30 }}
          onClick={() => handleNav('next')}
          src="https://upload.wikimedia.org/wikipedia/commons/thumb/d/d7/Arrow_right.svg/1280px-Arrow_right.svg.png"
        />
      </div>
    </div>
  );
};

export default MyCarousel;

@andelh
Copy link

andelh commented Apr 6, 2021

Did anyone get this working? @diegohennrich 's method didn't seem to do it for me :( .

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

No branches or pull requests

4 participants