【问题标题】:Value must be set for boolean attributes with Typescript and Material UI必须使用 Typescript 和 Material UI 为布尔属性设置值
【发布时间】:2019-10-05 15:57:13
【问题描述】:

我从 Material UI 中复制并粘贴了一段非常简单的代码,我正在尝试使用 Typescript。我有一个MediaCard 组件(重命名为DisplayCard)。

当我编译代码时,我收到这个错误:(34,23): Value must be set for boolean attributes

我很难找出这个错误的来源,因为我不确定需要设置哪个属性值...

代码如下:

App.tsx

import * as React from "react";
import DisplayCard from "./components/DisplayCard";

const App = () => {
  return <DisplayCard />;
};

export default App;

DisplayCard.tsx

import * as PropTypes from "prop-types";
import * as React from "react";

import { createStyles, withStyles } from "@material-ui/core/styles";

import Button from "@material-ui/core/Button";
import Card from "@material-ui/core/Card";
import CardActionArea from "@material-ui/core/CardActionArea";
import CardActions from "@material-ui/core/CardActions";
import CardContent from "@material-ui/core/CardContent";
import CardMedia from "@material-ui/core/CardMedia";
import Typography from "@material-ui/core/Typography";

const styles = createStyles({
  card: {
    maxWidth: 345
  },
  media: {
    height: 140
  }
});

function DisplayCard(props: any) {
  const { classes } = props;
  return (
    <Card className={classes.card}>
      <CardActionArea>
        <CardMedia
          className={classes.media}
          image='/static/images/cards/contemplative-reptile.jpg'
          title='Contemplative Reptile'
        />
        <CardContent>
          <Typography gutterBottom variant='h5' component='h2'>
            Lizard
          </Typography>
          <Typography component='p'>
            Lizards are a widespread group of squamate reptiles, with over 6,000
            species, ranging across all continents except Antarctica
          </Typography>
        </CardContent>
      </CardActionArea>
      <CardActions>
        <Button size='small' color='primary'>
          Share
        </Button>
        <Button size='small' color='primary'>
          Learn More
        </Button>
      </CardActions>
    </Card>
  );
}

DisplayCard.propTypes = {
  classes: PropTypes.object.isRequired
};

export default withStyles(styles)(DisplayCard);

【问题讨论】:

    标签: reactjs typescript material-ui typescript3.0


    【解决方案1】:

    我认为是指&lt;Typography gutterBottom variant='h5' component='h2'&gt;这一行。

    当您设置像gutterBottom 这样的属性时,它会被推断为gutterBottom={true}。既然你用的是TypeScript,大神们会因为你不显式而生气,所以尝试显式设置属性。

    <Typography gutterBottom={true} variant='h5' component='h2'>
    

    希望这将消除错误。

    【讨论】:

      猜你喜欢
      • 2021-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-08
      • 2019-03-17
      • 1970-01-01
      • 2017-09-08
      • 1970-01-01
      相关资源
      最近更新 更多