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.

ViewPager does not re-render correctly on deleting last index of the array

See original GitHub issue

Bug

Please excuse my English, explanation may be a little long before I actually talk about the problem (reproducible code at the end).

Currently, I have ViewPager setup with array of 4 items.

I have to delete an item(or equivalent to screen) in the array by clicking a button located at that screen, using the position returned from onPageSelected.

_onPressDelete = index => {
    let newArr = this.state.result;
    newArr.splice(index, 1);
    this.setState({result: newArr});
  };

Here is a simulation of the problem.

68726184-07e05680-0604-11ea-9bd8-91984509e810

For example, if I click the delete button in screen 2, component re-renders and next screen(screen 3) is shown as state changes. This should be the correct response. This is the next state after delete:

68726268-42e28a00-0604-11ea-9dfc-2d63263a607a

Deleting any screen from screen1 to screen 3 works without any problem since there is a screen next to the deleted screen. However problem occurs when I try to delete the last screen (screen 4). Since screen 4 is last screen and there is no screen 5 to move to, viewPager re-renders with just blank screen (with other bugs like showing deleted screen again)

This problem only occurs on ios and not on android, so I guess it is ios problem only. On Android, deleting the last index correctly falls back to the previous screen, e.g. screen 3. Any fixes to the problem?

Environment info

React native info output:

Android Studio: 3.5 AI-191.8026.42.35.5900203
Xcode: 11.0/11A420a - /usr/bin/xcodebuild
npmPackages:
react: 16.9.0 => 16.9.0
react-native: 0.61.4 => 0.61.4
npmGlobalPackages:
react-native-cli: 2.0.1

ViewPager Library version: 3.1.0

Steps To Reproduce

  1. I have provided a code below to try. It will be much easier to understand if you try the code

2.If you press delete button in screen 5, it will show blank as it re-renders. Other screens work perfectly fine. There is no problem on Android. …

Reproducible sample code

import React, {Component} from 'react';
import {
  StyleSheet,
  View,
  Text,
  Dimensions,
  Button,
} from 'react-native';
import ViewPager from '@react-native-community/viewpager';

class App extends Component {
  state = {
    result: ['screen 1', 'screen 2', 'screen 3', 'screen 4', 'screen 5'],
  };

  _onPressDelete = index => {
    let newArr = this.state.result;
    newArr.splice(index, 1);
    this.setState({result: newArr});
  };

  render() {
    return (
      <ViewPager style={styles.container} initialPage={0} showPageIndicator>
        {this.state.result.map((item, index) => {
          return (
            <View style={styles.pageItem} key={item}>
              <Text style={{fontSize: 20}}>{item}</Text>
              <Button
                title="delete"
                onPress={() => {
                  this._onPressDelete(index);
                }}
              />
            </View>
          );
        })}
      </ViewPager>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
  },
  pageItem: {
    width: Dimensions.get('window').width,
    height: Dimensions.get('window').height,
    backgroundColor: 'lightgrey',
    justifyContent: 'center',
  },
});

export default App;

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
rakeshvora2007commented, May 5, 2020

I am having the same issue as mentioned above. Is this bug solved or still in any progress? Is there anything I can help with?

1reaction
troZeecommented, Jun 19, 2020

I am having the same issue as mentioned above. Is this bug solved or still in any progress? Is there anything I can help with?

Hey @rakeshvora2007, I would really appreciate, if you can find some time and take a look on it.

I can see from @panda6412 's snippet, that VP need to be unmount and then mount again.

If you need any guidance, how to fix it, please let me know here in comments 😄

Thanks. Though do you know why it works fine On Android?

Because Android has different implementation than iOS. Both are purely native and can work differently. Of course on the end the effect is the same. JavaScript is unifying both platform into one common api.

Read more comments on GitHub >

github_iconTop Results From Across the Web

ViewPager does not re-render correctly on deleting last index ...
Currently, I have ViewPager setup with array of 4 items. I have to delete an item(or equivalent to screen) in the array by...
Read more >
react-native-viewpager issue on iOS while deleting the last ...
I am able to delete the last view/item from viewPager on android and it would switch the view to the previous screen.
Read more >
How to refresh data in ViewPager Fragment - Edureka
This is what I am trying to do: I have ViewPager in my Activity which hosts 6 Fragment. I disabled paging by swiping...
Read more >
Android Fragments Common Queries & Common Mistakes
When we are using BottomBarNavigation and NavigationDrawer people face issues like fragments recreated, same fragment is added multiple times ...
Read more >
PagerAdapter | Android Developers
Remove a page for the given position. The adapter is responsible for removing the view from its container, although it only must ensure...
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