Created
January 27, 2017 10:57
-
-
Save SudoPlz/256da5bafab903b8e6f4ae50df1bf1b1 to your computer and use it in GitHub Desktop.
How to deal with react-native-navigation global events (not the best way but it works)
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
/** | |
* A_Component.js | |
*/ | |
import { Record } from 'immutable'; | |
import React from 'react'; | |
import * as authActions from '../../state/auth/authActions'; | |
// import * as globalActions from '../../state/global/globalActions'; | |
/** | |
* Components | |
*/ | |
import createScreen from '../AcuityScreenFactory'; | |
import AComponentRender from './AComponentRender'; | |
class AComponent extends React.Component { | |
constructor(props) { | |
super(props); | |
} | |
render() { | |
return ( | |
<View /> | |
); | |
} | |
} | |
ƒ.propTypes = { | |
getBtnHeight: React.PropTypes.func.isRequired, | |
auth: React.PropTypes.instanceOf(Record), | |
device: React.PropTypes.instanceOf(Record), | |
push: React.PropTypes.func.isRequired, | |
replace: React.PropTypes.func, | |
actions: React.PropTypes.object, | |
}; | |
export default { | |
name: 'A_COMPONENT', | |
screen: () => createScreen([authActions], this.name, { | |
navigatorButtons: { | |
leftButtons: [], // buttons for the left side of the nav bar | |
rightButtons: [], // buttons for the right side of the nav bar | |
}, | |
})(AComponent), | |
}; |
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
/** | |
* SPScreenFactory.js | |
* The allmighty creator of all screens. | |
*/ | |
import { Map, Record } from 'immutable'; | |
import React from 'react'; | |
/** | |
* Redux | |
*/ | |
import { bindActionCreators } from 'redux'; | |
import { connect } from 'react-redux'; | |
// import _ from 'underscore'; | |
import * as routingActions from '../state/routing/routingActions'; | |
import { MIN_BTN_HEIGHT, Animation, ScheneKeys } from '../config'; | |
let btnHeight; | |
export default function createScreen(stateActions = [], screenName, navProps, stateProps) { | |
// _.findIndex(stateActions, stateAction => stateAction === routingActions); | |
/* if stateActions does NOT already contain routingActions, just add them */ | |
if (stateActions.find(stateAction => stateAction === routingActions) == null) { | |
stateActions.push(routingActions); | |
} | |
return (Component) => { | |
function mapStateToProps(state) { | |
if (stateProps) { | |
return { | |
...state, | |
...stateProps, | |
}; | |
} | |
return { | |
...state, | |
}; | |
} | |
function mapDispatchToProps(dispatch) { | |
const creators = Map() | |
.merge(...stateActions) | |
.filter(value => typeof value === 'function') | |
.toObject(); | |
return { | |
actions: bindActionCreators(creators, dispatch), | |
dispatch, | |
}; | |
} | |
class SPScreen extends React.Component { | |
componentWillReceiveProps(nextProps) { | |
// Check if we have a manual Replace of our Root Schene | |
if (nextProps.router.get('manualReplaceRootSchene') !== this.props.router.get('manualReplaceRootSchene')) { | |
this.replaceScreen(nextProps.router.get('manualReplaceRootSchene')); | |
} | |
} | |
getBtnHeight(height) { | |
if (!btnHeight) { | |
btnHeight = height || this.props.device.get('screenHeight') * 0.065; | |
btnHeight = btnHeight < MIN_BTN_HEIGHT ? btnHeight : MIN_BTN_HEIGHT; | |
} | |
return btnHeight; | |
} | |
showCardNotification(message, stylingProps, showForSeconds = 10) { | |
const props = stylingProps || {}; | |
this.props.navigator.showInAppNotification({ | |
screen: ScheneKeys.NOTIFICATION, | |
passProps: { message, ...props }, | |
animation: Animation.NOTIFICATION_SWING, | |
position: 'top', // or bottom | |
autoDismissTimerSec: showForSeconds, | |
dismissWithSwipe: true, | |
}); | |
} | |
pushScreen(params) { | |
if (params) { | |
if (typeof params === 'string') { | |
this.props.actions.navigateTo(params); | |
this.props.navigator.push({ screen: params, title: '' }); | |
} else if (params.screen) { | |
this.props.actions.navigateTo(params.screen); | |
this.props.navigator.push(params); | |
} | |
} | |
} | |
replaceScreen(params) { | |
if (params) { | |
if (typeof params === 'string') { | |
this.props.actions.navigateTo(params, true); | |
// console.log(`about to replace screen with ${params}`); | |
this.props.navigator.resetTo({ screen: params, title: '' }); | |
} else if (params.screen) { | |
this.props.actions.navigateTo(params.screen, true); | |
// console.log(`about to replace screen with ${JSON.stringify(params)}`); | |
this.props.navigator.resetTo(params); | |
} | |
} | |
} | |
popScreen(animated = true) { | |
this.props.actions.navigateToPrevious(); | |
this.props.navigator.pop({ animated }); | |
} | |
showModal(params) { | |
if (params) { | |
if (typeof params === 'string') { | |
this.props.actions.navigateTo(params, true); | |
// console.log(`about to replace screen with ${params}`); | |
this.props.navigator.showModal({ screen: params, title: '' }); | |
} else if (params.screen) { | |
this.props.actions.navigateTo(params.screen, true); | |
// console.log(`about to replace screen with ${JSON.stringify(params)}`); | |
this.props.navigator.showModal(params); | |
} | |
} | |
} | |
dismissModal(animationType) { | |
this.props.navigator.dismissModal({ | |
animationType: animationType || 'slide-down', // 'none' / 'slide-down' , dismiss animation for the modal (optional, default 'slide-down') | |
}); | |
} | |
toggleDrawer(forcedNewState) { | |
this.props.navigator.toggleDrawer(forcedNewState != null ? | |
{ | |
side: 'left', | |
animated: true, | |
to: forcedNewState, | |
} | |
: | |
{ | |
side: 'left', | |
animated: true, | |
}); | |
} | |
dispatchDeepLink(link) { | |
this.props.navigator.handleDeepLink({ link }); | |
} | |
render() { | |
return ( | |
<Component | |
{...this.props} | |
push={this.pushScreen.bind(this)} | |
pop={this.popScreen.bind(this)} | |
replace={this.replaceScreen.bind(this)} | |
showModal={this.showModal.bind(this)} | |
dismissModal={this.dismissModal.bind(this)} | |
getBtnHeight={this.getBtnHeight.bind(this)} | |
toggleDrawer={this.toggleDrawer.bind(this)} | |
navigator={this.props.navigator} | |
showNotification={this.showCardNotification.bind(this)} | |
dispatchDeepLink={this.dispatchDeepLink.bind(this)} | |
/>); | |
} | |
} | |
if (navProps && navProps.navigatorButtons) { | |
SPScreen.navigatorButtons = navProps.navigatorButtons; | |
} | |
SPScreen.propTypes = { | |
// notifications: React.PropTypes.instanceOf(Record).isRequired, | |
router: React.PropTypes.instanceOf(Record).isRequired, | |
// global: React.PropTypes.instanceOf(Record).isRequired, | |
navigator: React.PropTypes.object.isRequired, | |
actions: React.PropTypes.object.isRequired, | |
device: React.PropTypes.object.isRequired, | |
}; | |
return connect(mapStateToProps, mapDispatchToProps)(SPScreen); | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment