Last active
July 11, 2016 18:10
-
-
Save cpsubrian/278ad43f79f2301c4aba1cb5401fca99 to your computer and use it in GitHub Desktop.
react-router-native dynamic header via redux
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
export const MERGE_CONF = 'core.app.merge_conf' | |
export function mergeConf (conf) { | |
return {type: MERGE_CONF, conf} | |
} |
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 {View, Image, Text, TouchableHighlight} from 'react-native' | |
import {Link} from 'react-router-native' | |
import Scene from 'core/components/scene/Scene' | |
import styles from './HomeScene.styles' | |
class HomeScene extends React.Component { | |
render () { | |
return ( | |
<Scene title='Home'> | |
<View style={[styles.scene, styles.homeScene]}> | |
<Link to='/states' style={styles.space}> | |
<Text style={styles.h1}>States --></Text> | |
</Link> | |
</View> | |
</Scene> | |
) | |
} | |
} | |
export default HomeScene |
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
const routes = ( | |
/* Address Bar can be toggled on or off by setting the addressBar prop */ | |
<Router history={nativeHistory} addressBar> | |
<TabsRoute path="app" component={App}> | |
<Route path='/' component={HomeScene} overlayComponent={NavigationHeader}/> | |
<Route path='/states' component={StatesScene} overlayComponent={NavigationHeader}/> | |
</TabsRoute> | |
<Route path="*" component={NoMatch}/> | |
</Router> | |
); | |
AppRegistry.registerComponent('YourApp', () => () => routes); |
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 Immutable from 'immutable' | |
import { | |
MERGE_PROPS | |
} from 'core/actions/scene' | |
const initialState = Immutable.fromJS({ | |
props: {} | |
}) | |
const reducers = { | |
[MERGE_PROPS]: (state, {props}) => { | |
return state.mergeIn(['props'], props) | |
} | |
} | |
export default {name: 'scene', initialState, reducers} |
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 _ from 'lodash' | |
import {connect} from 'react-redux' | |
import {mergeProps} from 'core/actions/scene' | |
@connect(null, {mergeProps}) | |
class Scene extends React.Component { | |
static propTypes = { | |
title: React.PropTypes.string, | |
mergeProps: React.PropTypes.func, | |
children: React.PropTypes.node | |
} | |
componentDidMount () { | |
this.props.mergeProps(_.omit(this.props, 'mergeProps', 'children')) | |
} | |
componentWillReceiveProps (nextProps) { | |
this.props.mergeProps(_.omit(nextProps, 'mergeProps', 'children')) | |
} | |
render () { | |
return this.props.children | |
} | |
} | |
export default Scene |
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 {createSelector} from 'reselect' | |
export const sceneSelector = createSelector( | |
(state) => state.scene, | |
(scene) => { | |
return { | |
sceneProps: scene.get('props').toJS() | |
} | |
} | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment