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.

Problem on re-rendering of component, setting a new firstItem index

See original GitHub issue

I have several miniComponents listed in my carousel and a user can select and deselect these miniComponents on different screens to be visible in the carousel or not. Removing and adding them works splendidly (the miniComponents.map function below).

So an actual rerendering is on this page with the Carousel happening.

But if remove one of the miniComponents, my activeMiniCompIndex might chance as components now move to different positions. I set in firstItem the activeMiniCompIndex, the Carousel stays however on the same position. I checked via console.log output that the activeMiniCompIndex is changing and that a rerender is executed in the component containing the carousel, however the carousel is not.

<Carousel
            ref={carousel => {
              this._carousel = carousel;
            }}
            firstItem={activeMiniCompIndex}
            sliderWidth={Dimensions.get('window').width}
            itemWidth={Dimensions.get('window').width * 0.75}
            scrollEndDragDebounceValue={0}
            inactiveSlideOpacity={0.8}
            contentContainerCustomStyle={S.carouselContainer}
            onSnapToItem={index => {
              this.activeMiniCompKeyStorage.set(miniComponents[index].key);
              if (index !== activeMiniCompIndex) {
                tracker.trackEvent('miniComponents', 'scrollTo', { label: miniComponents[index].name });
                this.setState({ activeMiniCompKey: miniComponents[index].key });
              }
            }}
          >
            {miniComponents.map((miniComponent, index) =>
              <View key={index}>
                {this.renderContent(miniComponent)}
              </View>
            )}
</Carousel>

I checked your documentation and past issues, unfortunately I couldn’t find a solution to this problem.

Thank you in advance for your help!

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:29 (15 by maintainers)

github_iconTop GitHub Comments

3reactions
bd-arccommented, Aug 29, 2017

Hi @KevKo1990,

Just a quick thought, but does it work if you add extraData={this.state} to the <Carousel />? (from FlatList’s doc)

And if it doesn’t, can you set up a simple test case so I can easily reproduce the issue?

1reaction
bd-arccommented, Sep 14, 2017

Hey @KevKo1990,

You can either copy/paste the raw code, like you did, or reference the commit in your package.json file:

"react-native-snap-carousel": "https://github.com/archriss/react-native-snap-carousel#f38ef07"

and then run npm install or yarn depending on what you’re using.

Regarding the issue, what you’re showing is the expected behavior: if you remove an item, the closest one becomes active. And, as I’ve previously mentioned, since the callback is not fired the Pagination component won’t automatically update and will fall back to its default value (0).

I think that it’s a little dangerous to always go back to the first item if the current one is deleted. Imagine a list of hundreds of items; it would result in a pretty bad user experience.

What would be the best behavior in your opinion?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Problem on re-rendering of component, setting a new firstItem ...
I have several miniComponents listed in my carousel and a user can select and deselect these miniComponents on different screens to be ...
Read more >
Component not re-rendering after change in an array state in ...
A solution could be the spread operator, which will create a copy of your existing array but on a new memory reference ,...
Read more >
How and when to force a React component to re-render
React automatically re-renders components, but what happens when a component is not updating as expected? Find out what you can do here.
Read more >
How to stop re-rendering lists in React? - Alex Sidorenko
First, let's simplify our example by removing all props from the Item . We will still update the parent state but won't pass...
Read more >
5 Ways to Avoid React Component Re-Renderings
In this article, I have discussed 5 different methods to prevent unnecessary re-rendering in React components. Most of these solutions capitalize caching, and ......
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