Created
September 8, 2018 23:39
-
-
Save brickpop/ffcaefd0cf4c164b223d7ea4c53a4e27 to your computer and use it in GitHub Desktop.
Wrapper to fix a bug in ViewPagerAndroid when combined with React Navigation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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