artydev is right about the fact that the code could be optimized but Matt is also right. Adding a library does not optimize this piece of code.
Having a look at the code there is only one thing that changes and that is text in the last h1 tag. So if you want to optimize this code why not do it like this?
<!DOCTYPE html><htmllang="en"><head><title>State Management in Vanilla JS</title></head><body><h1>Hello Vanilla JS!</h1><div>
Example of state management in Vanilla JS
</div><br/><h1id="app"></h1><buttonid="button">Increase</button><script>constApp=function_App(){return_App.state.count;};App.state={count:0,increment:()=>{App.state.count++;App.state.updateUI();},updateUI:()=>{document.getElementById("app").textContent=App();}};App.state.updateUI();document.getElementById("button").addEventListener("click",App.state.increment);</script></body></html>
There is no need to add the event listener on every update and there is no need to rerender the rest of the html on every update. Just update the text in the h1 tag and minimize the rerendering needs. If you want to build the html from javascript you can render the html just once on initialization.
No problem artydev. You can of course always fall back on a library when you have a lot of re-rendering going on. I believe that to make the right decision you need to understand what is going on under the hood. There are a lot of tutorials on diffing and VDom to be found on the internet for those interested.
You are absolutely right. artydev and Matt Kimek started a discussion about optimization. I just hooked in to that. Your post was about handling state in vanilla javascript and you did a good job on that.
Hello Matt,
Please ask real questions, read authors posts, share your tips, examples
That will be constructive for all of us
Regards
First of all great post Vijay!
artydev is right about the fact that the code could be optimized but Matt is also right. Adding a library does not optimize this piece of code.
Having a look at the code there is only one thing that changes and that is text in the last h1 tag. So if you want to optimize this code why not do it like this?
There is no need to add the event listener on every update and there is no need to rerender the rest of the html on every update. Just update the text in the h1 tag and minimize the rerendering needs. If you want to build the html from javascript you can render the html just once on initialization.
Thank you for your tips
You are totally right if you have
few elements to update.
Regards
No problem artydev. You can of course always fall back on a library when you have a lot of re-rendering going on. I believe that to make the right decision you need to understand what is going on under the hood. There are a lot of tutorials on diffing and VDom to be found on the internet for those interested.
Hi there Viridi. Thanks for your input.
But optimising and moving all the elements to HTML wasn't the point of this article
Hi Vijay Pushkin,
You are absolutely right. artydev and Matt Kimek started a discussion about optimization. I just hooked in to that. Your post was about handling state in vanilla javascript and you did a good job on that.
And only for the pleasure , here is a case when using a library worth it :-)
You can test it here :
MulipleCounters