SHARE
TWEET

Untitled

a guest Aug 25th, 2019 55 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. export function updateDomProperties(dom, prevProps, nextProps) {
  2.   const isEvent = name => name.startsWith("on");
  3.   const isAttribute = name => !isEvent(name) && name != "children";
  4.  
  5.   // Remove event listeners
  6.   Object.keys(prevProps)
  7.     .filter(isEvent)
  8.     .forEach(name => {
  9.       const eventType = name.toLowerCase().substring(2);
  10.       dom.removeEventListener(eventType, prevProps[name]);
  11.     });
  12.  
  13.   // Remove attributes
  14.   Object.keys(prevProps)
  15.     .filter(isAttribute)
  16.     .forEach(name => {
  17.       dom[name] = null;
  18.     });
  19.  
  20.   // Set new attributes
  21.   Object.keys(nextProps)
  22.     .filter(isAttribute)
  23.     .forEach(name => {
  24.       dom[name] = nextProps[name];
  25.     });
  26.    
  27.   // Set new eventListeners
  28.   Object.keys(nextProps)
  29.     .filter(isEvent)
  30.     .forEach(name => {
  31.       const eventType = name.toLowerCase().substring(2);
  32.       dom.addEventListener(eventType, nextProps[name]);
  33.     });
  34. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top