我有一个使用服务器端渲染(SSR)来渲染React应用的Express服务器。
我想将materialize-css
软件包与我的react应用程序一起使用。
我已经将materialize-css/dist/css/materialize.min.css
和materialize-css/dist/js/materialize.min
导入了我的App.js。
在我使用express渲染我的应用程序服务器端之前,它工作正常,物化的样式和模式已正确加载。
现在,当我尝试使用express渲染应用程序时,出现以下错误:
[0] ReferenceError: window is not defined
[0] at Object.<anonymous> (/Users/user/Sites/task_keeper/node_modules/materialize-css/dist/js/materialize.min.js:6:1135)
[0] at Module._compile (internal/modules/cjs/loader.js:778:30)
[0] at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
[0] at Module.load (internal/modules/cjs/loader.js:653:32)
[0] at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
[0] at Function.Module._load (internal/modules/cjs/loader.js:585:3)
[0] at Module.require (internal/modules/cjs/loader.js:692:17)
[0] at require (internal/modules/cjs/helpers.js:25:18)
[0] at eval (webpack:///external_%22materialize-css/dist/js/materialize.min%22?:1:18)
这是我的渲染中间件:
const renderMiddleware = () => (req,res) => {
let html = req.html;
const htmlContent = ReactDOMServer.renderToString(<App />);
const htmlReplacements = {
HTML_CONTENT: htmlContent,};
Object.keys(htmlReplacements).forEach(key => {
const value = htmlReplacements[key];
html = html.replace(
new RegExp('__' + escapeStringRegexp(key) + '__','g'),value
);
});
res.send(html);
};
然后我在index.js
中使用水合物
ReactDOM.hydrate(<App />,document.getElementById('root'));.
在这样的设置中使用materialize-css
的正确方法是什么?