Reputation: 39
Event summary:
I click edit button.
editForm() displays the form using this.setState() and this.state inside render().
The setState() above has the {this.state} as a value that I need to display.
Once I click update button inside the form, updateMessage() activates, which Meteor.call that includes a callback option.
This callback function has this.setState() that connects to that {this.state} I mentioned above.
So how do I make that {this.state} display after Meteor.call() callback and setState()?
-note- putting that {this.state} inside render() will display after callback.
Heres the code: this.state.show_error_or_noerror inside editMessage() is what I need to display.
constructor(props) {
super(props);
const messageContent = this.props.messageContent;
const username = this.props.username;
const articleTitle = this.props.articleTitle;
this.state = {
show_error_or_noerror: '',
messageContent: messageContent,
username: username,
articleTitle: articleTitle
};
this.editMessage = this.editMessage.bind(this);
this.updateMessage = this.updateMessage.bind(this);
}
updateMessage(event) {
event.preventDefault();
const messageId = this.props.messageId;
const messageContent = this.refMessage.value.trim();
Meteor.call('message.update', messageId, messageContent, (error) => {
if(error) {
this.setState({show_error_or_noerror: error.reason});
} else {
this.setState({show_error_or_noerror: 'Updated successfully.'});
}
});
}
editMessage(event) {
event.preventDefault();
const messageId = this.props.messageId;
this.setState({
['show_form'+messageId]: <form onSubmit={this.updateMessage}>
<textarea
ref={(textarea) => {this.refMessage = textarea;}}
defaultValue={this.state.messageContent}
/>
<h6>by {this.state.username}</h6>
<h6>Article: {this.state.articleTitle}</h6>
<button type="submit">Update</button>
<button onClick={this.hideForm}>Hide</button>
{this.state.show_error_or_noerror}
</form>
});
}
render() {
const messageId = this.props.messageId;
return (
<span className="message_updator">
<button onClick={this.editMessage}>Edit</button>
{this.state['show_form'+messageId]}
</span>
)
}
}
Upvotes: 0
Views: 428
Reputation: 543
To achieve reactivity in meteor, create a Tracker.dependency object and have your render depend on it (ref).
Create a dependency in your constructor
var dep = new Tracker.Dependency;
And have your render depend on it
dep.depend();
And call this in your setState function
dep.changed();
Upvotes: 0