Skip to content

Instantly share code, notes, and snippets.

@Tevinthuku
Created August 13, 2019 19:43
Show Gist options
  • Select an option

  • Save Tevinthuku/2c8b0889abea549cdaac738bbfe636e7 to your computer and use it in GitHub Desktop.

Select an option

Save Tevinthuku/2c8b0889abea549cdaac738bbfe636e7 to your computer and use it in GitHub Desktop.
export function updateDomProperties(dom, prevProps, nextProps) {
const isEvent = name => name.startsWith("on");
const isAttribute = name => !isEvent(name) && name != "children";
// Remove event listeners
Object.keys(prevProps)
.filter(isEvent)
.forEach(name => {
const eventType = name.toLowerCase().substring(2);
dom.removeEventListener(eventType, prevProps[name]);
});
// Remove attributes
Object.keys(prevProps)
.filter(isAttribute)
.forEach(name => {
dom[name] = null;
});
// Set new attributes
Object.keys(nextProps)
.filter(isAttribute)
.forEach(name => {
dom[name] = nextProps[name];
});
// Set new eventListeners
Object.keys(nextProps)
.filter(isEvent)
.forEach(name => {
const eventType = name.toLowerCase().substring(2);
dom.addEventListener(eventType, nextProps[name]);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment