I have a fully functional social media app and an API made with Express, Pug and JavaScript which is server-side rendered.
For example, when I comment on a post the comment is added but I have to manually reload the page for it to be displayed.
I’m wondering if there is a way to display and update the content without the need to reload the page using JS?
Or should I just make the app client-side rendered with react?
This is the link for the app https://github.com/Fedechini/kleory
2
Answers
Yes, you have to make the app client-side rendered. In template engine you cant use react hooks, I mean you have to use
useState, useEffect
to load comments without loading the page.If you do not want a page refresh, you have to use Ajax to submit the new comment, and to display the new comment in the page you need to use dynamic Html with client side scription.
It should be something like the following which doesn’t work in JSFiddle:
A summary of the steps when user clicks the Submit button:
<textarea>
.fetch
(only in new browsers) call to perform an asynchronous Http POST request to send the new comment to your backend to insert into your database./:postId/comments
function should return a Json response with the new comment is successfully inserted.fetch
callback receives the newly inserted comment from the backend and displays it in the<span>
element.