【发布时间】:2017-03-20 09:44:38
【问题描述】:
很抱歉,如果以前报告过此问题,我已尝试在 Google 上找到解决方案并在此处搜索问题,但找不到描述我的问题的内容。
我在 React 的控制台中收到一个错误,提示客户端和服务器标记不匹配并且校验和失败。这会导致 FOUC。
考虑以下来源:
// MyComponent.scss
.myComponent {
background: black;
}
// MyComponent.js
import styles from './MyComponent.scss';
const MyComponent = () => <div className={styles.myComponent}>Hello</div>;
export default MyComponent;
在客户端上,这按预期工作,styles.myComponent 将呈现为 MyComponent-myComponent-[hash],如您所料。
但是,在服务器上 styles.myComponent 未定义,因此省略了类名,这就是导致控制台错误的原因,因为 DOM 结构不匹配。我已经记录了样式对象,看起来服务器上有一个locals 对象,这是我找到渲染组件名称的地方。例如
styles: {
locals: {
myComponent: 'MyComponent-myComponent-[hash]'
}
}
我不知道从哪里开始寻找为什么导入的 css 模块对象在客户端和服务器上看起来不同。
【问题讨论】:
标签: reactjs server-side-rendering css-modules