Teact: Fix textContent optimization

This commit is contained in:
Alexander Zinchuk 2023-07-20 15:58:04 +02:00
parent a2eb398feb
commit 7daee68c0e
2 changed files with 30 additions and 3 deletions

View File

@ -0,0 +1,24 @@
import type { FC } from '../../lib/teact/teact';
import React, { useState } from '../../lib/teact/teact';
// 1. Make sure "First line" is rendered even if followed by a component with single text.
// 2. Make sure it then can be normally removed (target is preserved).
// 3. Make sure "Last line" is also rendered.
const Text: FC = () => {
return 'text component';
};
export function App() {
const [withFirstLine, setWithFirstLine] = useState(true);
return (
<div onClick={() => setWithFirstLine((current) => !current)}>
{withFirstLine && 'First line'}
<Text />
Last line
</div>
);
}
export default App;

View File

@ -129,9 +129,12 @@ function renderWithVirtual<T extends VirtualElement | undefined>(
mountChildren(parentEl, $new as VirtualElementComponent | VirtualElementFragment, { nextSibling, fragment });
} else {
const canSetTextContent = (
!fragment && !nextSibling && $newAsReal.type === VirtualType.Text && $parent.children.length === 1
);
const canSetTextContent = !fragment
&& !nextSibling
&& $newAsReal.type === VirtualType.Text
&& $parent.children.length === 1
&& !parentEl.firstChild;
if (canSetTextContent) {
parentEl.textContent = $newAsReal.value;
$newAsReal.target = parentEl.firstChild!;