Created
October 17, 2019 23:21
-
-
Save shinchit/1ec241b654c7f3e9c46b153741a75fe9 to your computer and use it in GitHub Desktop.
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 from 'react'; | |
import * as Localization from 'expo-localization'; | |
import { StackActions, NavigationActions } from 'react-navigation'; | |
import lodash from 'lodash'; | |
const withCommonProps = (WrappedComponent) => { | |
class HOC extends React.Component { | |
getLanguageCode = () => { | |
return Localization.locale.split('-')[0]; | |
} | |
navigateTo = (routes) => { | |
const { navigation } = this.props; | |
let actions = lodash.reduce( | |
!lodash.isArray(routes) ? [routes] : lodash.reverse(routes), | |
(result, route) => { | |
let action = result; | |
if (!(action instanceof Object)) { | |
action = NavigationActions.navigate({ routeName: action }); | |
} | |
return NavigationActions.navigate({ | |
routeName: route, | |
action, | |
}); | |
}, | |
); | |
if (!lodash.isArray(routes)) { | |
actions = NavigationActions.navigate({ routeName: actions }); | |
} | |
const resetAction = StackActions.reset({ | |
index: 0, | |
actions: [ | |
actions, | |
], | |
}); | |
navigation.dispatch(resetAction); | |
} | |
render() { | |
return ( | |
<WrappedComponent | |
getLanguageCode={this.getLanguageCode} | |
navigateTo={this.navigateTo} | |
{...this.props} | |
/> | |
); | |
} | |
} | |
return HOC; | |
}; | |
export default withCommonProps; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment