admin管理员组文章数量:1226619
Using React 18.3.1, I'm trying to SSR & hydrate an app but it seems even the most basic component runs into hydration issues.
My component looks like this:
function App() {
const [count, setCount] = useState(0);
return <button onClick={() => setCount(count + 1)}>count is {count}</button>;
}
if (typeof window !== 'undefined') {
const target = document.getElementById('app');
hydrateRoot(target, <App />);
}
It correctly renders into HTML, creating <div id="app"><button>count is 0</button></div>
on the server. However, when it goes to hydrate, I'm left with the following hydration issue:
Warning: Text content did not match. Server: "count is 0" Client: "count is "
(using React dev mode to get a usable error message)
It seems the compiled JSX splits the text node in two, which React then trips over:
function ja() {
const [k, O] = Bc.useState(0);
return Pi.jsxs('button', {
onClick: () => O(k + 1),
children: [
'count is ',
k
]
})
}
But I'm confused as to why this is and how I'm meant to be fixing this. Why can't React handle its own JSX transform?
Using React 18.3.1, I'm trying to SSR & hydrate an app but it seems even the most basic component runs into hydration issues.
My component looks like this:
function App() {
const [count, setCount] = useState(0);
return <button onClick={() => setCount(count + 1)}>count is {count}</button>;
}
if (typeof window !== 'undefined') {
const target = document.getElementById('app');
hydrateRoot(target, <App />);
}
It correctly renders into HTML, creating <div id="app"><button>count is 0</button></div>
on the server. However, when it goes to hydrate, I'm left with the following hydration issue:
Warning: Text content did not match. Server: "count is 0" Client: "count is "
(using React dev mode to get a usable error message)
It seems the compiled JSX splits the text node in two, which React then trips over:
function ja() {
const [k, O] = Bc.useState(0);
return Pi.jsxs('button', {
onClick: () => O(k + 1),
children: [
'count is ',
k
]
})
}
But I'm confused as to why this is and how I'm meant to be fixing this. Why can't React handle its own JSX transform?
Share Improve this question edited Feb 6 at 7:18 DarkBee 15.6k8 gold badges70 silver badges115 bronze badges asked Feb 6 at 7:17 Devildude4427Devildude4427 1,0951 gold badge11 silver badges33 bronze badges1 Answer
Reset to default 1It turns out that React inserts comment nodes to support this children format, so the generated HTML should look like this instead:
<div id="app"><button>count is <!-- -->0</button></div>
However, I was using node-html-parser
to generate my HTML doc, not realizing it removed comments by default. So renderToString
was in fact returning the correct content but it was stripped out on my end.
本文标签: reactjsHydration error when text node is split between static text amp state valueStack Overflow
版权声明:本文标题:reactjs - Hydration error when text node is split between static text & state value - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1739418703a2162411.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论