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 :)