Skip to content

Instantly share code, notes, and snippets.

@brickpop
Created September 8, 2018 23:39
Show Gist options
  • Save brickpop/ffcaefd0cf4c164b223d7ea4c53a4e27 to your computer and use it in GitHub Desktop.
Save brickpop/ffcaefd0cf4c164b223d7ea4c53a4e27 to your computer and use it in GitHub Desktop.
Wrapper to fix a bug in ViewPagerAndroid when combined with React Navigation
import React, { PureComponent } from 'react';
import {
View,
Dimensions,
Platform
} from 'react-native';
export default class ViewPagerWrapper extends PureComponent {
constructor(props) {
super(props);
this._x = 0.5;
this.state = {
width: Dimensions.get('window').width,
};
this.navigationEventFocus = this.props.navigation.addListener(
'didFocus',
() => {
if (Platform.OS != "android") return;
setTimeout(() => this._reattach(), 100);
}
);
}
componentWillUnmount() {
this.navigationEventFocus.remove();
}
render() {
return (
<View style={[this.props.style, { flex: 1 }]}>
<View style={{ width: this.state.width, flex: 1 }}>
{this.props.children}
</View>
</View>
)
}
_reattach = () => {
this.setState({
width: this.state.width - this._x,
}, () => {
this._x *= -1;
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment