Skip to content

Commit

Permalink
upsertElement
Browse files Browse the repository at this point in the history
  • Loading branch information
ericfortis committed Sep 6, 2023
1 parent 9bae1dd commit f80742a
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions react-create-element/createElement.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,24 @@ function useRef() {
return { current: null }
}


// V2
function upsertElement(elem, props, ...children) {
const node = elem instanceof HTMLElement
? elem
: document.createElement(elem)
for (const [key, value] of Object.entries(props))
if (key === 'ref')
value.current = node
else if (key === 'style')
Object.assign(node.style, value)
else if (key.startsWith('on'))
node.addEventListener(key.replace(/^on/, '').toLowerCase(), value)
else if (key in node)
node[key] = value
else
node.setAttribute(key, value)
node.append(...children)
return node
}

0 comments on commit f80742a

Please sign in to comment.