Created
November 4, 2019 04:33
-
-
Save shinchit/5becb61a489d996bc5275c08e84af26a 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 { | |
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 /> | |
); | |
} | |
} |
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 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); |
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 { | |
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); |
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'; | |
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