How to Refresh Page using Lightning Web Component
Hi Trailblazer,
I came across a scenario where I need to refresh the whole page after we perform the logic from an inline LWC. After hitting hard I found below solutions.
- Use Web Component inside Aura Component and then dispatch the event from the LWC which will be handled by parent AURA Component and then from Aura Component refresh the page.
- Use eval JavaScript function to refresh the page from the LWC itself. Below is the code for the same, copy and paste the code after the line from where you wanted to refresh the page.
eval("$A.get('e.force:refreshView').fire();");
Thanks so much for this one! any pointers on what exactly the above javascript function does?
Hi Ajay,
You can get the complete details from here:- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval
This is Aura component, not LWC. Go back to reading books.
Thanks for your comment however this is working and the syntax is completely different. Why do not your post by your name instead of going anynomous.
I think you didnt understand what the author was saying. LWC doesnt have a force:refreshView event but aura does. We also know that we can host an LWC inside an aura. This point revolves around these 2 points. I think the author has read sufficient books and has significant experience to be able to present this ingenious idea. If you do not agree present your arguments rationally rather than posting unnecessarily rude comments.
The problem is that the first point mentions an Aura Component. The second point, by NOT mentioning an Aura Component, makes it sound like you don’t need one. If in fact it does, that should be clarified.
window.location.reload();
We can use above javascript code directly inside LWC to reload the page.
Yes, And this will reload the whole page not just the soft refresh
This works great! Thanks for the same!
Hi SFDC Panther, “eval is evil” we should never use it, it is an open door to XSS Scripting Attacks. Below is the link
https://medium.com/@eric_lum/the-dangerous-world-of-javascripts-eval-and-encoded-strings-96fd902af2bd.
Please let us know what do you think about it.