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.

Forward embla up in the hierarchy using React

See original GitHub issue

First of all, congratulations on developing Embla, It’s a great library!

I’m trying to interact programmatically with Embla from a component that it’s higher in a React component hierarchy, but I cannot figure how to do it. I have a <Container> component that renders another component with the carousel (<EmblaCarousel>), but I want to use a hook in <Container> (potentially useEffect), that access the carousel and calls its scrollTo(). I cannot nest the <Container> in EmblaCarousel>. Is there a way to do this? I tried using React.forwardRef in <EmblaCarousel>, but I couldn’t figure how to use it to achieve what I have in mind.

Thanks in advance!

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
davidjerlekecommented, Oct 24, 2020

No worries Ian (@ianmethyst),

Thanks for the response. I wast curious if it helped and I’m glad to hear that it did, so I’m closing this issue as resolved. I pretty much followed the example in the official documentation page for React.forwardRef.

About the off-topic request. I don’t think your app will suffer a performance hit unless you’re calling embla.reInit() repeatedly, like 100 times a second or similar. You’re welcome to create a feature request for this, although I can’t promise that I will choose to add it to the Embla Carousel core.

Kindly, David

1reaction
ametis70commented, Oct 23, 2020

Thank you for your time, and sorry for the late response! Yes, this solved my problem, I didn’t realize I could just move the hook up and use React.forwardRef this way

The use case for the off topic question is that I have a carousel that have some items that are zoomable and draggable, and I’d like to disable the drag event in the carousel when an image is zoomed and enable it again when the zoom is reset. I thought I could use reinit, but I was wondering if this would have some kind of performance impact. Sorry if this is a silly question and using reInit is the way to go, I didn’t have the time to test it out

Read more comments on GitHub >

github_iconTop Results From Across the Web

Forward embla up in the hierarchy using React · Issue #111
I have a <Container> component that renders another component with the carousel ( <EmblaCarousel> ), but I want to use a hook in...
Read more >
Forwarding Refs
Ref forwarding is a technique for automatically passing a ref through a component to one of its children. This is typically not necessary...
Read more >
Lifting State Up
In React, sharing state is accomplished by moving it up to the closest common ancestor of the components that need it. This is...
Read more >
Thinking in React
Step 1: Break The UI Into A Component Hierarchy ... The first thing you'll want to do is to draw boxes around every...
Read more >
Higher-Order Components
Use HOCs For Cross-Cutting Concerns · Don't Mutate the Original Component. Use Composition. · Convention: Pass Unrelated Props Through to the Wrapped Component....
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