Last active
May 28, 2020 18:59
-
-
Save AregSargsyan/cd308a80ce5e228b6daa9eb68ebf187d to your computer and use it in GitHub Desktop.
Change detection with plain JS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<div id="dataDiv"></div> | |
<button id="btn" (click)="handler()"> ClickMe </button> | |
<script> | |
let count = 0; | |
// initial rendering | |
detectChange(); | |
function renderHTML() { | |
document.getElementById('dataDiv').innerText = count; | |
} | |
function detectChange() { | |
const currentValue = document.getElementById('dataDiv').innerText; | |
if (currentValue !== count) { | |
renderHTML(); | |
} | |
} | |
// update data inside button click event handler | |
document.getElementById('btn').addEventListener('click', () => { | |
// update count | |
count ++; | |
// call detectChange manually | |
detectChange(); | |
}); | |
</script> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment