firasd / react-server-tutorial

Code from the React tutorial, modified to add Server-Side Rendering, React Router, and Redux.
https://medium.com/@firasd/quick-start-tutorial-universal-react-with-server-side-rendering-76fe5363d6e
Other
74 stars 14 forks source link

Why the comments requests are executed only once #2

Closed maiopirata closed 8 years ago

maiopirata commented 8 years ago

There is something I'm missing, and I would like to understand why.

The CommentBox component has the onSubmit method and also the old fashion action method. When we have javascript enabled the route /api/client/ should receive 2 post request. One in Json format and one in html.

Normally e.preventDefault() will stop the execution of the server-side request. In order to test I commented e.preventDefault() and with my surprise the comment is still added only once. I'm quite curious about why this happens.

So I thought that on server side when the first /api/comments POST is received the res.redirect trigger a page reloading and this probably kills all the ajax (client-side requests)..

In order to test that I put the res.redirect function inside a setTimeout of 3 sec. And know what? the comment is still not showing twice.. Is that the form submit that kills the ajax call?

maiopirata commented 8 years ago

Ok sorry I answered myself! In the case js enabled, the server side code does not execute since the setState({author:"" etc}) is executed and the form submit on server side has empty values..

I got it now