Skip to content

Instantly share code, notes, and snippets.

@shinchit
Created November 4, 2019 04:33
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/5becb61a489d996bc5275c08e84af26a to your computer and use it in GitHub Desktop.
Save shinchit/5becb61a489d996bc5275c08e84af26a to your computer and use it in GitHub Desktop.
import React from 'react';
import {
createAppContainer,
} from 'react-navigation';
import {
createStackNavigator,
} from 'react-navigation-stack';
const stack = createStackNavigator(
{
A: ScreenA,
B: ScreenB,
},
{
/* set global options */
defaultNavigationOptions: ({ navigation }) => ({
headerTintColor: '#fff',
headerBackTitle: null,
}),
},
);
const App = createAppContainer(stack);
export default class Whatever extends React.Component {
render() {
return (
<App />
);
}
}
import React from 'react';
import withCommonProps from './withCommonProps';
class ScreenA extends React.Component {
constructor(props) {
super(props);
}
componentDidMount() {
const { navigation } = this.props;
navigation.setParams({
title: 'title of ScreenA',
});
}
}
export default withCommonProps(ScreenA);
import React from 'react';
import {
Button,
Text,
} from 'react-native';
import withCommonProps from './withCommonProps';
class ScreenB extends React.Component {
constructor(props) {
super(props);
}
componentDidMount() {
const { navigation, navigateTo } = this.props;
navigation.setParams({
title: 'title of ScreenB',
headerLeft: () => {
return (
<Button
transparent
onPress={() => {
navigateTo('A');
}}
>
<Text style={{ color: '#fff' }}>Back</Text>
</Button>
);
},
});
}
}
export default withCommonProps(ScreenB);
import React from 'react';
const withCommonProps = (WrappedComponent) => {
class HOC extends React.Component {
static navigationOptions = ({ navigation }) => {
return {
title: navigation.getParam('title'),
headerLeft: navigation.getParam('headerLeft'),
};
};
// write some common functions
// e.g.
// navigateTo = (routes) => {
// Details are omitted.
// see. https://www.its-all-writing.com/entry/2019/10/18/083529
// }
}
return HOC;
};
export default withCommonProps;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment