question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Help with the carousel's initial load using useEffect()

See original GitHub issue

Hi @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:closed
  • Created 3 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
davemullenjnrcommented, Mar 7, 2021

Hey @davidcetinkaya,

The new site looks awesome! Really beautiful work. Huge fan of the simplicity and looks great in dark mode. 👏

2reactions
davemullenjnrcommented, Feb 25, 2021

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found