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.

this._carousel.currentIndex is undefined

See original GitHub issue

How can I store a state with the index of the currently active slide?

I have tried this._carousel.currentIndex, but I get the error undefined is not an object (evaluating 'this._carousel.currentIndex').

I am setting the ref with

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

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:22 (8 by maintainers)

github_iconTop GitHub Comments

2reactions
rotalex2000commented, Jun 9, 2021

The problem is that you try to use the currentIndex property of the carousel before the Carousel component is rendered and the reference is set.

Make carousel a state variable: const [carousel, setCarousel] = useState(null);

and use setCarousel() to set it in the component. <Carousel ref={c => { setCarousel(c); }} ... />

Then simply check for it’s nullity everytime you use it, so that you catch the errors that might appear before the Carousel component loads and sets the reference.

2reactions
jwan3453commented, Nov 14, 2018

@hesusdios , simply change the _renderItem to arrow function like _renderItem = ({item, index}) => {} will solve the problem, or renderItem={this._renderItem.bind(this)}.

Read more comments on GitHub >

github_iconTop Results From Across the Web

React-Native-snap-carousel check condition with state value?
In render item I can't use state or props value. How can I check with current index value in render item method? constructor(props)...
Read more >
react-native-reanimated-carousel - npm
Simple carousel component.fully implemented using Reanimated 2.Infinitely scrolling, very smooth.. Latest version: 3.1.5, last published: 2 ...
Read more >
Tiny Slider 2 - GitHub Pages
Option Type Description axis “horizontal” | “vertical” Default: “horizontal”. The axis of the slider. gutter positive integer Default: 0. Space between slides (in “px”). edgePadding positive...
Read more >
How to build a multi-image carousel in React and Tailwind
Important because the component is likely to rerender by clicking the prev/next buttons. currentIndex - this is a simple state value that will ......
Read more >
How to Handle Infinite Loop in React Carousel
After disabling the animation, we need to change the currentIndex of the Carousel. After the currentIndex is modified, then we enable the ...
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