From 464d949467c4c16bd396661d3880813d0cba6f3b Mon Sep 17 00:00:00 2001 From: Renard Date: Mon, 16 Sep 2019 01:53:15 -0300 Subject: setState is async --- src/ReplyForm.js | 77 ++++++++++++++++++++++++++++++++------------------------ 1 file changed, 44 insertions(+), 33 deletions(-) (limited to 'src') diff --git a/src/ReplyForm.js b/src/ReplyForm.js index 33a6af4..6695406 100644 --- a/src/ReplyForm.js +++ b/src/ReplyForm.js @@ -24,41 +24,52 @@ class ReplyForm extends Component { } handleSubmit() { - const { submittedName, submittedEmail, name, email, message } = this.state; - this.setState({ - submittedName: name, - submittedEmail: email, - submittedMessage: message - }); - const data = { - board: this.props.dir, - parent: this.props.parent, - name: "", - email: "", - fielda: submittedName, - fieldb: submittedEmail, - message: message, - password: "bai-client" - }; + const { + submittedName, + submittedEmail, + submittedMessage, + name, + email, + message + } = this.state; + this.setState( + { + submittedName: name, + submittedEmail: email, + submittedMessage: message + }, + () => { + const data = { + board: this.props.dir, + parent: this.props.parent, + name: "", + email: "", + fielda: submittedName, + fieldb: submittedEmail, + message: submittedMessage, + password: "bai-client" + }; - const formData = new FormData(); - for (var key in data) { - formData.append(key, data[key]); - } + const formData = new FormData(); + for (var key in data) { + formData.append(key, data[key]); + } - fetch("https://bienvenidoainternet.org/cgi/api/post", { - method: "POST", - mode: "cors", - redirect: "follow", - body: formData - }) - .then(response => { - return response.json(); - }) - .then(resource => { - console.log(resource); - this.setState({ replyRes: resource }); - }); + fetch("https://bienvenidoainternet.org/cgi/api/post", { + method: "POST", + mode: "cors", + redirect: "follow", + body: formData + }) + .then(response => { + return response.json(); + }) + .then(resource => { + console.log(resource); + this.setState({ replyRes: resource }); + }); + } + ); } render() { -- cgit v1.2.1-18-gbd029