Meh*_*hdi 24 reactjs material-ui vite
我使用 vite 和 React 创建了一个项目。我创建一个主题将我的项目设置为从右到左。一切正常,项目运行正常。
const theme = createTheme({
direction: 'rtl' ,
typography: {
"fontFamily": `"iransans"`,
"fontSize": 11,
"fontWeightLight": 300,
"fontWeightRegular": 400,
"fontWeightMedium": 500
}
})
const cacheRtl = createCache({
key: "muirtl",
stylisPlugins: [prefixer, rtlPlugin]
})
function App() {
let history = useHistory();
let contained = "Test The";
return (
<div className="App">
<Router>
<div>
<Switch>
<CacheProvider value={cacheRtl}>
<ThemeProvider theme={theme}>
<Route exact path="/applicant">
<Applicant />
</Route>
</ThemeProvider>
</CacheProvider>
</Switch>
</div>
</Router>
</div>
)
}
export default App
Run Code Online (Sandbox Code Playgroud)
将 Slide 组件添加到我的项目后。突然我的项目停止工作,控制台显示
Box.js:5 未捕获类型错误:createTheme_default 不是 Box.js:5:22 的函数
我回滚我的更改(通过 git 检查)。但错误仍然显示。
我不明白发生了什么事,为什么在恢复更改后错误仍然存在?
小智 30
不使用 ThemeProvider 只是简单的 MUI。我发现错误来自我导入 CSSBaseline 的 app.tsx 页面。
将 CSSBaseline 移动到首页上的 Box import 上方可以解决该问题:
从:
import Box from "@mui/material/Box";
import CssBaseline from "@mui/material/CssBaseline";
Run Code Online (Sandbox Code Playgroud)
到:
import CssBaseline from "@mui/material/CssBaseline";
import Box from "@mui/material/Box";
Run Code Online (Sandbox Code Playgroud)
ReF*_*ity 11
对我来说,这是因为导入了 MUIBox组件:
import Box from '@mui/material/Box';
import { BoxProps } from '@mui/material/Box/Box';
import { useTheme } from '@mui/material/styles';
Run Code Online (Sandbox Code Playgroud)
我把它改为
import { Box } from '@mui/material';
import { BoxProps } from '@mui/material';
import { useTheme } from '@mui/material';
Run Code Online (Sandbox Code Playgroud)
它起作用了
小智 8
我遇到了同样的错误,所以我删除了所有材料 ui 依赖项并重新安装它们,它对我有用。
yarn remove @mui/material @emotion/react @emotion/styled
Run Code Online (Sandbox Code Playgroud)
或者
npm uninstall @mui/material @emotion/react @emotion/styled
Run Code Online (Sandbox Code Playgroud)
小智 5
主要错误是由于在访问组件之前没有获取主题提供程序的可用性,或者在其他情况下是 vite 缓存问题。
要解决第一种情况,请将您的应用程序组件包装在主题提供程序中。
import { ThemeProvider, createTheme } from '@mui/material/styles';
const theme = createTheme({
direction: 'rtl',
// other theme properties
});
function App() {
return (
<ThemeProvider theme={theme}>
<div className="App">
...
</div>
</ThemeProvider>
)
};
Run Code Online (Sandbox Code Playgroud)
现在,如果出现第二个问题,请使用标志重新启动服务器--force。
例如:npm run dev --force
| 归档时间: |
|
| 查看次数: |
25147 次 |
| 最近记录: |