Last active
April 13, 2016 00:11
-
-
Save ide/05afe70c25acc75a3719 to your computer and use it in GitHub Desktop.
Using an event emitter to communicate between the scene and navigation bar button
This file contains hidden or 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
function getProfileRoute(user) { | |
let emitter = new EventEmitter(); | |
return { | |
renderScene(navigator) { | |
let ProfileScreen = require('./ProfileScreen'); | |
return ( | |
<ProfileScreen | |
user={user} | |
navigator={navigator} | |
routeEvents={emitter} | |
/> | |
); | |
}, | |
renderRightButton() { | |
return <EditButton routeEvents={emitter} />; | |
}, | |
} | |
} | |
class EditButton extends React.Component { | |
constructor(props, context) { | |
super(props, context); | |
this.state = { isEditing: false }; | |
} | |
render() { | |
return( | |
<TouchableOpacity | |
touchRetentionOffset={ExNavigator.Styles.barButtonTouchRetentionOffset} | |
onPress={this._handlePress.bind(this)} | |
style={ExNavigator.Styles.barRightButton}> | |
<Text style={ExNavigator.Styles.barRightButtonText}> | |
{this.state.isEditing ? 'Done' : 'Edit'} | |
<Text/> | |
</TouchableOpacity> | |
); | |
} | |
_handlePress() { | |
let shouldBeEditing = !this.state.isEditing; | |
this.setState({ isEditing: shouldBeEditing }); | |
// Communicate with other components with the event emitter | |
this.props.routeEvents.emit('editing', shouldBeEditing); | |
} | |
// To receive events, add a listener in componentDidMount and remove it in | |
// componentWillUnmount. | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment