this._carousel.currentIndex is undefined
See original GitHub issueHow 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:
- Created 6 years ago
- Comments:22 (8 by maintainers)
Top 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 >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 FreeTop 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
Top GitHub Comments
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.
@hesusdios , simply change the _renderItem to arrow function like _renderItem = ({item, index}) => {} will solve the problem, or renderItem={this._renderItem.bind(this)}.