Skip to content

Instantly share code, notes, and snippets.

@Schniz
Last active April 8, 2018 02:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Schniz/f3b031cad3f48c5d70c451449ab67d66 to your computer and use it in GitHub Desktop.
Save Schniz/f3b031cad3f48c5d70c451449ab67d66 to your computer and use it in GitHub Desktop.
export const navigationOptions = (defaults = {}) => Component => class extends React.Component {
static navigationOptions = ({ state }) => ({
...defaults,
...(state.params.navigationOptions || {}),
})
render() {
return (
<Component
NavigationOptions={NavigationOptions(this.props.navigation)}
{...this.props}
/>
)
}
}
export default navigationOptions
exports default navigation => class NavigationOptions extends Component {
componentDidMount() {
this.setParams(this.props)
}
componentWillReceiveProps(nextProps) {
this.setParams(nextProps)
}
setParams = (navigationOptions) => {
navigation.setParams({ navigationOptions })
}
render() {
return children || null
}
}
/// USAGE
@navigationOptions({
title: "Default title",
})
class MyScreen extends Component {
saveFn = () => {}
render() {
const { NavigationOptions } = this.props;
<NavigationOptions
title="Another title"
headerRight={<Button title="save" onPress={this.saveFn} />}
>
<View><Text>My View</Text></View>
</NavigationOptions>
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment