Skip to content

Commit 64bca9e

Browse files
authored
[iOS] fix: crash on empty array (#152)
1 parent a0c407a commit 64bca9e

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
lines changed

example/ViewPagerExample.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,11 @@ export default class ViewPagerExample extends React.Component<*, State> {
8787
}));
8888
};
8989

90+
removeLastPage = () => {
91+
this.setState(prevState => ({
92+
pages: prevState.pages.slice(0, prevState.pages.length - 1),
93+
}));
94+
};
9095
move = (delta: number) => {
9196
const page = this.state.page + delta;
9297
this.go(page);
@@ -152,6 +157,11 @@ export default class ViewPagerExample extends React.Component<*, State> {
152157
onPress={this.toggleDotsVisibility}
153158
/>
154159
<Button enabled={true} text="Add new page" onPress={this.addPage} />
160+
<Button
161+
enabled={true}
162+
text="Remove last page"
163+
onPress={this.removeLastPage}
164+
/>
155165
</View>
156166
<View style={styles.buttons}>
157167
{animationsAreEnabled ? (

example/src/component/ProgressBar.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,13 @@ export class ProgressBar extends React.Component<Props> {
2323
render() {
2424
const fractionalPosition =
2525
this.props.progress.position + this.props.progress.offset;
26-
const progressBarSize =
27-
(fractionalPosition / (this.props.numberOfPages - 1)) * this.props.size;
26+
27+
let progressBarSize = this.props.size;
28+
if (this.props.numberOfPages !== 1) {
29+
progressBarSize =
30+
(fractionalPosition / (this.props.numberOfPages - 1)) * this.props.size;
31+
}
32+
2833
return (
2934
<View style={[styles.progressBarContainer, {width: this.props.size}]}>
3035
<View style={[styles.progressBar, {width: progressBarSize}]} />

ios/ReactNativePageView.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ - (void)layoutSubviews {
238238
}
239239

240240
- (void)didUpdateReactSubviews {
241-
if (_childrenViewControllers.count == 0){
241+
if (self.reactSubviews.count == 0) {
242242
return;
243243
}
244244
[self addPages];
@@ -384,7 +384,7 @@ - (UIViewController *)createChildViewController:(UIView *)view {
384384
}
385385

386386
- (void)goTo:(NSNumber *)index animated:(BOOL)animated {
387-
if (_currentIndex >= 0) {
387+
if (_currentIndex >= 0 && _currentIndex < _childrenViewControllers.count) {
388388
UIPageViewControllerNavigationDirection direction =
389389
(index.integerValue > _currentIndex)
390390
? UIPageViewControllerNavigationDirectionForward

0 commit comments

Comments
 (0)