【问题标题】:Material Ui code is working perfectly in codepen but not in VSCODE?Material Ui 代码在 codepen 中完美运行,但在 VSCODE 中却不行?
【发布时间】:2019-05-29 11:44:14
【问题描述】:

我正在使用 Material-UI 在我的 ReactJS 项目中创建“材质选项卡”,此代码在 SANDBOX 中正常工作,但在我的 VS CODE 中却没有。我该怎么办?

我检查了要安装的 Node,检查并安装来自 NPM 的所有依赖项版本。 我也检查过了。 check it,不过看不懂。

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 版本?
  • 我在本地机器上的 VSCode 中运行相同的代码并且没有出现任何错误,除了 dir proptypes
  • @B4BIPIN 解决问题了吗?
  • 不,@HRK44,我将材料版本从“^3.9.2”升级到“^4.0.1”。但这不起作用。

标签: javascript reactjs material-ui


【解决方案1】:

在您的沙箱中,您使用的是@material-ui/core latest 版本(当前为4.0.1),而在您的VS Code package.json 中,它的版本为3.9.2。我很确定这是问题所在,请尝试使用完全相同的版本,而不是使用 latest

请看这里:https://codesandbox.io/embed/material-demo-1j37n

【讨论】:

  • 但是当我在 React 官方网站上搜索时,他们说 React Hooks(在我的代码中 makeStyles)只用于函数,而不是类。 check
  • 请检查。使用函数Function,使用类Class
  • @B4BIPIN 在沙箱中使用正确的版本可以正常工作,并且错误与您提到的使用较低版本的错误相同。这似乎很奇怪,它不适用于最新的 4.x 版本
  • 是的,你是对的。由于依赖项冲突而发生此问题。但是当我创建一个新项目并匹配依赖项时,我意识到了这一点。感谢@HRK44
猜你喜欢
  • 2022-06-13
  • 1970-01-01
  • 2018-02-07
  • 2013-01-06
  • 2016-11-18
  • 1970-01-01
  • 1970-01-01
  • 2018-02-08
相关资源
最近更新 更多