javascript - Material Ui 代码在 codepen 中完美运行,但在 VSCODE 中却不行?
问题描述
我正在使用 Material-UI 在我的 ReactJS 项目中创建“材质选项卡”,此代码在 SANDBOX 中正常工作,但在我的 VS CODE 中却没有。我应该怎么办?
我检查了要安装的 Node,检查并安装来自 NPM 的所有依赖项版本。我也检查过了。检查它,但我无法理解。
import React from "react";
import PropTypes from "prop-types";
import SwipeableViews from "react-swipeable-views";
import { makeStyles } from "@material-ui/core/styles";
import { withStyles } from "@material-ui/styles";
import Tabs from "@material-ui/core/Tabs";
import Tab from "@material-ui/core/Tab";
import Typography from "@material-ui/core/Typography";
function TabContainer({ children, dir }) {
return (
<Typography component="div" dir={dir} style={{ padding: 8 * 3 }}>
{children}
</Typography>
);
}
const useStyles = makeStyles(theme => ({
root: {
backgroundColor: theme.palette.background.paper,
width: 500
}
}));
class Feature extends React.Component {
constructor(props) {
super(props);
this.state = { value: 0 };
}
handleChange() {
this.state.value === 0
? this.setState({ value: 1 })
: this.setState({ value: 0 });
}
render() {
const classes = this.props;
const theme = this.props;
return (
<div className={classes.root}>
<Tabs
value={this.state.value}
onChange={this.handleChange.bind(this)}
indicatorColor="primary"
textColor="primary"
>
<Tab label="A" />
<Tab label="B" />
</Tabs>
<SwipeableViews
axis={theme.direction === "rtl" ? "x-reverse" : "x"}
index={this.state.value}
>
<TabContainer dir={theme.direction}>Item One</TabContainer>
<TabContainer dir={theme.direction}>Item Two</TabContainer>
</SwipeableViews>
</div>
);
}
}
Feature.propTypes = {
classes: PropTypes.object.isRequired
};
export default withStyles(useStyles)(Feature);
我的package.json看起来像
"dependencies": {
"@material-ui/core": "^3.9.2",
"@material-ui/docs": "^3.0.0-alpha.9",
"@material-ui/styles": "^4.0.1",
"prop-types": "^15.7.2",
"react": "^16.8.5",
"react-dom": "^16.8.5",
"react-redux": "^6.0.1",
"react-router-dom": "^5.0.0",
"react-scripts": "2.1.8",
"react-slick": "^0.23.2",
"react-swipeable-views": "^0.13.3",
"redux": "^4.0.1",
"styled-components": "^4.2.0"
}
我期望SANDBOX给出的输出,但得到了这个错误
TypeError: Object(...) is not a function
const useStyles = makeStyles(theme => ({
root: {
backgroundColor: theme.palette.background.paper,
width: 500
}
}));
在上面的代码中。
解决方案
在您的沙箱中,您使用的是@material-ui/core latest
version (当前是4.0.1
),而在您的 VS Code package.json 中是 version 3.9.2
。我很确定这是问题所在,请尝试使用完全相同的版本而不是使用latest
.
推荐阅读
- ruby-on-rails - 如何使用嵌套形式的值设置外键?
- css - iphone Mozilla上没有显示谷歌支付按钮
- c# - 如何从字符串中提取起始字符?
- java - 将控制台输出发送到日志文件
- http - 离子 3 标头请求未在 post 方法中发送
- scala - 区分 Google 的默认图片和上传图片
- fileserver - 有没有办法为 Nextcloud 中除管理员以外的所有用户禁用通过链接共享复选框?
- javascript - 搜索引擎中的Logo是什么?
- javascript - Javascript获取动画以跟随路径并保存画布状态
- angular - 如何在Angular中的http Post请求后保存JSON数据