Help with the carousel's initial load using useEffect()
See original GitHub issueHi @davidcetinkaya,
Thanks so much for offering to help with this part, I know it’s kinda outside of the scope of Embla specifically so you really don’t have to hold my hand like this but it’d be much appreciated to learn how to configure this useEffect
function.
Here’s the Carousel.js
component
import React, { useEffect } from 'react';
import { useEmblaCarousel } from 'embla-carousel/react';
import styled from 'styled-components';
const Wrapper = styled.section`
height: auto;
overflow: hidden;
.is-draggable {
cursor: move;
cursor: grab;
}
.is-dragging {
cursor: grabbing;
}
`;
const Container = styled.div`
display: flex;
width: 100vw;
justify-content: stretch;
`;
const CarouselWrapper = ({ children }) => {
const [emblaRef, emblaApi] = useEmblaCarousel({
loop: true,
});
useEffect(() => {
if (emblaApi) {
// Embla API is ready
}
}, [emblaApi]);
return (
<Wrapper>
<div className="embla" ref={emblaRef}>
<Container className="embla__container">
{children}
</Container>
</div>
</Wrapper>
);
};
export default CarouselWrapper;
Then each slide is just a gatsby-image
wrapped in a div from this CarouselSlide.js
component.
export const CarouselSlide = ({ image, alt }) => {
return (
<div className="embla__slide">
<Img fluid={image} alt={alt} />
</div>
);
};
I hope that makes sense, let me know if I need to include anything else. I think I just need an example of what to put in the useEffect
bit.
Thanks in advance Dave
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Building a carousel component in React using Hooks
In this article, we look at implementing a carousel that tries to simplify the entanglement by using React Hooks.
Read more >Using the Effect Hook - React
We pass a function to the useEffect Hook. This function we pass is our effect. Inside our effect, we set the document title...
Read more >How to call loading function with React useEffect only once
If you only want to run the function given to useEffect after the initial render, you can give it an empty array as...
Read more >React useEffect Hook usages you must know - GreenRoots Blog
Learn the practical usages of useEffect Hook in React. This article shows you how to manage a component's side effects with various use...
Read more >How to Use React Suspense to Improve Your UI Load Time
React Suspense is a great way to improve the load time of your UI. In this post, learn about Suspense and why you...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Hey @davidcetinkaya,
The new site looks awesome! Really beautiful work. Huge fan of the simplicity and looks great in dark mode. 👏
Amazing! That’s worked perfectly, thank you so much. It’s now in production: hoodiehut.co.uk
That’s great to see how it works, too. Really appreciate the help with this. 🙏