简体   繁体   English

create-react-app / Material-UI 应用程序样式在部署时有所不同

[英]create-react-app / Material-UI App Styling Different on Deploy

I had a certain bug that I was unsure how to debug.我有一个我不确定如何调试的错误。 The bug is that styling that I use for a create-react-app (Typescript) app through material-ui and regular CSS, shows up just fine in development but it shows differently for certain CSS properties when I deploy the app online (I've only tried Heroku and Vercel).错误是我通过 material-ui 和常规 CSS 用于create-react-app (Typescript) 应用程序的样式在开发中显示得很好,但是当我在线部署应用程序时,某些 CSS 属性的显示方式有所不同(我我只试过 Heroku 和 Vercel)。 The app doesn't show any errors or warnings in the console when I'm developing.在我开发时,该应用程序不会在控制台中显示任何错误或警告。 I have no idea why it's happening and I've tried the following so far我不知道为什么会这样,到目前为止我已经尝试了以下方法

  1. Read through my code many times being extra careful at every step.多次通读我的代码,每一步都格外小心。
  2. Open the website (development and production) in incognito mode on Chrome 85.0.4183.83 (Linux, 64 bit)在 Chrome 85.0.4183.83(Linux,64 位)上以隐身模式打开网站(开发和生产)
  3. Look through the documentation for material-ui (material-ui.com) and the React docs for any lead on this查看 material-ui (material-ui.com) 的文档和 React 文档以了解有关此方面的任何线索
  4. Search google for prior such issues在谷歌搜索之前的此类问题

So far, nothing has turned up.到目前为止,什么都没有出现。 Could someone please guide me in the right direction?有人可以指导我正确的方向吗?

The build pack I use on Heroku after deployment is the well known one by mars and available at https://buildpack-registry.s3.amazonaws.com/buildpacks/mars/create-react-app.tgz .部署后我在 Heroku 上使用的构建包是 mars 众所周知的构建包,可在https://buildpack-registry.s3.amazonaws.com/buildpacks/mars/create-react-app.tgz 获得

For convenience, I have included my package.json here on pastebin.为了方便起见,我已经包括了我package.json这里的引擎收录。

Thank you so much for helping out.非常感谢您的帮助。

============= EDIT ============= ============== 编辑 ==============

I've been experimenting with the code and it seems like the problem lies with how material-ui is compiled during build time.我一直在试验代码,似乎问题在于在构建时如何编译 material-ui。 The problems which don't show up in development, show up in production.在开发中没有出现的问题,在生产中出现了。

For example, the biggest thing I noticed is if you mix custom style prop flags given by the material-ui components and add your own styling with className or style , during build time these latter styles are overridden partially if not completely.例如,我注意到的最重要的事情是,如果您混合由 material-ui 组件提供的自定义样式道具标志,并使用classNamestyle添加您自己的样式,则在构建期间,这些样式会被部分覆盖(如果不是完全覆盖)。

So if I do所以如果我这样做

import   React,
       { useRef }  from "react";
import {
         Button,
         makeStyle,
         Theme
       }           from "@material-ui/core";
/*
Start of functional component using memo
.
.
.
*/

const useClasses = useRef(makeStyle((theme : Theme) => { return {
      buttonStyles:
      {
          backgroundColor: theme.palette.text.hint
      }
}}));

const styles = useClasses.current();

<Button
    color={"secondary"}
    className={styles.buttonStyles}>
    button text
</Button>
/*
.
.
.
*/

in development, the button will have the backgroundColor I give it in makeStyles , but in production that property will switch to secondary as given in the color prop which defaults to the value theme.palette.secondary.main .在开发中,该按钮将具有backgroundColor我给它在makeStyles ,但在生产该属性将切换到secondary如在给定的color丙默认为值theme.palette.secondary.main

And sometimes, just even using a component from material-ui without any extra config causes a problem.有时,即使使用 material-ui 中的组件而没有任何额外配置也会导致问题。

I am also using code splitting with lazy and Suspense if that matters.如果重要的话,我还使用lazySuspense进行代码拆分。

StackMatch can you please link your github/gitlab repo? StackMatch 你能链接你的 github/gitlab 仓库吗? I think I might know what the problem is but can't be certain without seeing how your dev environment is configured.我想我可能知道问题是什么,但如果不了解您的开发环境是如何配置的,就无法确定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM