Skip to content

Instantly share code, notes, and snippets.

@arqex
Last active May 16, 2017 14:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arqex/cde9f7a1b541567f1767e26e7e7fd2db to your computer and use it in GitHub Desktop.
Save arqex/cde9f7a1b541567f1767e26e7e7fd2db to your computer and use it in GitHub Desktop.
Returning reaction
// We can return the promise from our reaction
freezer.on('message:send', text => {
freezer.get().set({status: 'SENDING'});
// Return the promise
return Ajax.post('/messages', {text: text} )
.then( msg => {
freezer.get()
.set({status: 'READY'})
.messages.push(msg)
;
})
;
});
// in a different file, our component will trigger the message sending
// and show the success message when it finish
class MessageEditor extends React.Component {
render(){
return (
<div>
<textarea onChange={ e => this.setState({msg: e.target.value}) />
<button onClick={ () => this.send() }>Send</button>
</div>
);
}
send(){
// Receive the promise from the reaction, so we can handle the message
// displaying in the Component itself
freezer.emit('message:send', this.state.msg)
.then( () => this.showToast('Your message has been sent successfully!') )
;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment