Skip to content

Instantly share code, notes, and snippets.

@ide
Last active April 13, 2016 00:11
Show Gist options
  • Save ide/05afe70c25acc75a3719 to your computer and use it in GitHub Desktop.
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
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