Skip to content

Instantly share code, notes, and snippets.

@shinchit
Created October 17, 2019 23:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shinchit/1ec241b654c7f3e9c46b153741a75fe9 to your computer and use it in GitHub Desktop.
Save shinchit/1ec241b654c7f3e9c46b153741a75fe9 to your computer and use it in GitHub Desktop.
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