Les*_*ont 6 javascript reactjs material-ui
我试图用材料的UI的theme.spacing功能的应用程序做出反应,但间距功能无法识别。
Javascript错误消息是:TypeError:theme.spacing不是函数
我不确定这是否是错误或正在安装的框架版本有问题。
paddingTop: theme.spacing(20)根据package-lock.json文件,这些是正在安装的框架版本:
对于material ui version 5.1.0,以下内容对我有用(正如@Worm所说)
import { makeStyles } from '@mui/styles';
Run Code Online (Sandbox Code Playgroud)
记得安装@mui/styles
如果你收到类似警告
MUI: The `styles` argument provided is invalid.
You are providing a function without a theme in the context.
One of the parent elements needs to use a ThemeProvider.
MUI: The `styles` argument provided is invalid.
You are providing a function without a theme in the context.
One of the parent elements needs to use a ThemeProvider.
Run Code Online (Sandbox Code Playgroud)
使用 ThemeProvider 包装父容器
export default ParentComponentName = (props)=>{
return(
<ThemeProvider theme={theme}>
<SomeThemeComponent>
<SomeComponent />
</SomeThemeComponent>
<ThemeProvider>
)
}
Run Code Online (Sandbox Code Playgroud)
https://mui.com/styles/api/#examples-4
PS:由于信用限制,我无法发表评论,因此作为新答案发布。