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.

Display `this._carousel.currentIndex`

See original GitHub issue

I don’t understand currentIndex.

If I put it inside an onPress method, it works as it should, but I am not able to print it in a <Text>.

It says that this._carousel is undefined.

I am defining this._carousel with

<Carousel
  ref={carousel => { this._carousel = carousel; }}
  ...

See the code below.

elements.map((el, i) => {
  return (
    <TouchableOpacity onPress={() => alert(this._carousel.currentIndex)}>
      <Text>{this._carousel.currentIndex}</Text>
    </TouchableOpacity>
  )
})

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

6reactions
benjdlambertcommented, Aug 20, 2017

Easiest solution is not to use the ref.

The reason it doesn’t work, is the component itself doesn’t get re-rendered, which means that it will always display the value at render time.

A better solution would be to use onSnapToItem to set state inside the component that uses the carousel. Something like this, (not tested, just writing from memory)

class MyThing extends React.PureComponent {
  state = {
    currentIndex: 0,
  }
  changeIndex = (currentIndex) => {
    this.setState({ currentIndex });
  }
  render() {
    return (
      <View>
        <Carousel onSnapToItem={this.changeIndex} />
        <Text>{this.state.currentIndex</Text>
      </View>
  }
}

Think that should do it…

0reactions
algatecommented, Sep 28, 2021

Use onBeforeSnapToItem instead of onSnapToItem

The last project does not execute this programmatically;

Read more comments on GitHub >

github_iconTop Results From Across the Web

Display `this._carousel.currentIndex` · Issue #116 - GitHub
I don't understand currentIndex. If I put it inside an onPress method, it works as it should, but I am not able to...
Read more >
Display current index of iCarousel in a label which is not a ...
The index for a given item view in the carousel. Works for item views and placeholder views, however placeholder view indexes do not...
Read more >
How to build a multi-image carousel in React and Tailwind
You can view the finished multi-item carousel here ... We check to see if the currentIndex value is greater than zero and if...
Read more >
ngx-hm-carousel - npm
A light carousel for Angular, support mobile touch by hammerJs with SSR. ... export class DragOneComponent { currentIndex = 0; speed = 5000; ......
Read more >
How to Show Multiple Item in Simple React Carousel
Tagged with react, carousel, javascript, css. ... show-${show}`} + style={{ transform: `translateX(-${currentIndex * (100 / show)}%)` ...
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