-
Notifications
You must be signed in to change notification settings - Fork 47.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Clarify reason for setTextContent helper #11813
Conversation
return; | ||
} | ||
} | ||
node.textContent = text; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i'm not sure this is safe, but it seems to be. We use straight sets on textContent elsewhere
also, there seems to be some duplication or extra work being done in react/packages/react-dom/src/client/ReactDOMFiberComponent.js Lines 639 to 644 in 8be5a8b
seems to be essentially duplicated lower down in the react/packages/react-dom/src/client/ReactDOMFiberComponent.js Lines 690 to 699 in 8be5a8b
|
} | ||
} else if (typeof nextProp === 'number') { | ||
setTextContent(domElement, '' + nextProp); | ||
domElement.textContent = '' + nextProp; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why stringify here and not line 295?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
textContent
applies stringify and sanitization AFAIK
@@ -340,7 +338,7 @@ function updateDOMProperties( | |||
} else if (propKey === DANGEROUSLY_SET_INNER_HTML) { | |||
setInnerHTML(domElement, propValue); | |||
} else if (propKey === CHILDREN) { | |||
setTextContent(domElement, propValue); | |||
domElement.textContent = propValue; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Likewise, should we stringify?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the stringify happens in diffProperties for update, so the value is already a string here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Got it. In any case, this looks consistent with the old behavior. 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems good to me, is there anything I can help to verify?
I'm not sure how to verify honestly. It's looks to me like the setTextContent was legacy considering the "modern" usages just use text Content, but perhaps someone with more perspective knows? In particular that one branch is really special, I was assuming it was for ie8 (per the comment) but I'm not sure the acceptance criteria for that |
@@ -670,7 +668,7 @@ export function diffProperties( | |||
} | |||
for (propKey in nextProps) { | |||
const nextProp = nextProps[propKey]; | |||
const lastProp = lastProps != null ? lastProps[propKey] : undefined; | |||
const lastProp = lastProps[propKey]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just thinking about moving carefully, is this change related to the text content change? Could we send this out separately?
I know that's tip toeing quite a bit. Just trying to figure out how to move forward with confidence :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good eye :P yeah it's not related to the one change, just another thing I noticed. I did split it out by commit but we can do separate PR's
No harm in being intentional about the changes!
@jquense |
@jquense EDIT: Specifically because |
Awesome, thanks for the context @syranide Anyone else wanna jump in? otherwise good to go? |
I don't think this code is currently IE8-motivated. The IE8 part was removed in #11331. The current code using |
that's really interesting. The idea is that if the content is a single text node it's faster to set nodeValue there? I'd be happy try and verify, at the very least I'll update the comment :) |
It's still much faster to update a single text node via |
TIL :) Thanks for the clarification (and sorry i didn't find the previous work on this before updating!) |
@trueadm Just curious, could it possibly be faster with |
update the comment explaining the reason for the helper
@syranide I tried with |
@@ -736,7 +737,7 @@ const DOMRenderer = ReactFiberReconciler({ | |||
}, | |||
|
|||
resetTextContent(domElement: Instance): void { | |||
domElement.textContent = ''; | |||
setTextContent(domElement, ''); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Semi related:
#11609
commitTextUpdate
throws in some cases in IE9. I wonder if using this method there would fix it.
I can also confirm |
@trueadm Just for posterity, you should absolutely not use |
@syranide I’m well aware. http://perfectionkills.com/the-poor-misunderstood-innerText/. |
* Update comment on setTextContent update the comment explaining the reason for the helper * Use `setTextContent` in ReactDOM for consistency
* Update comment on setTextContent update the comment explaining the reason for the helper * Use `setTextContent` in ReactDOM for consistency
Some little thing’s I noticed, referencing the code here writing some of react-dom-lite