【发布时间】:2019-06-30 09:24:11
【问题描述】:
运行gatsby build 后,我在静态文件上遇到了一个奇怪的问题。
DOM 的属性(如 className)无法通过监听 prop 更改来更新,但 DOM 的内容(如文本或 DOM 的子项)则不能更新。
- 仅在 gatsby-build 之后发生,也就是在 SSR 中
// 版本 1,不工作
const ThemeProvider = ({ isLight, children }) => {
return (
<div className={isLight ? 'light-theme' : 'dark-theme'}> // <- does not change when `isLight` updating
<h1>{isLight ? 'light-theme' : 'dark-theme'}</h1> // <- changes when `isLight` updating
{children}
</div>
)
}
// 版本 2,不工作
// still having the same issue
const ThemeProvider = ({ isLight, children }) => {
if (isLight)
return (
<div className="light-theme">
<h1>{isLight ? 'light-theme' : 'dark-theme'}</h1>
{children}
</div>
)
return (
<div className="dark-theme">
<h1>{isLight ? 'light-theme' : 'dark-theme'}</h1>
{children}
</div>
)
}
// 版本 3,工作中
const ThemeProvider = ({ isLight, children }) => {
if (isLight)
return (
<div className="light-theme">
<h1>{isLight ? 'light-theme' : 'dark-theme'}</h1>
{children}
</div>
)
return (
<section className="dark-theme"> // <-- change to the different DOM, everything works fine
<h1>{isLight ? 'light-theme' : 'dark-theme'}</h1>
{children}
</section>
)
}
【问题讨论】:
-
您能分享更多您的网站设置,例如 gatsby-config 和 gatsby-ssr 吗? minimal, verifiable example 将不胜感激