Ошибка проверки PropTypes при выполнении приложения - PullRequest
0 голосов
/ 13 декабря 2018

Я пытаюсь использовать react hooks в простом приложении.Это ссылка на код, который я написал: MyApp

 const styles = theme => ({
    root: {
        ...theme.mixins.gutters(),
        paddingTop: theme.spacing.unit * 2,
        paddingBottom: theme.spacing.unit * 2
      },
      button: {
        margin: theme.spacing.unit
      }
 });

function PaperSheet(props) {
  const [open, setOpen] = useState(false);
  const { classes } = props;
  const { title, data } = props;

  return (
    <div>
      <Paper className={classes.root} elevation={1}>
        <Typography align="center" variant="h3" component="h3">
          {title}
        </Typography>
        <Typography align="center" component="p">
          What is the result of :
          {`${math.ceil(data[0])} + ${math.ceil(data[1])}`}
        </Typography>
        <TextField
          id="outlined-full-width"
          label="Result"
          style={{ margin: 8 }}
          fullWidth
          margin="normal"
          variant="outlined"
          InputLabelProps={{
            shrink: true,
            required: true
          }}
        />
        <Button
          variant="contained"
          color="primary"
          className={classes.button}
          onClick={() => {
            setOpen(true);
          }}
        >
          Submit
        </Button>
        <Modal open={open} closeModel={() => setOpen(false)} />
      </Paper>
    </div>
  );
}

PaperSheet.propTypes = {
  classes: PropTypes.objectOf(PropTypes.string.isRequired).isRequired,
  title: PropTypes.string.isRequired,
  data: PropTypes.arrayOf(PropTypes.number.isRequired).isRequired
};

function getModalStyle() {
  const top = 50;
  const left = 50;

  return {
    top: `${top}%`,
    left: `${left}%`,
    transform: `translate(-${top}%, -${left}%)`
  };
}

const styles = theme => ({
  paper: {
    position: "absolute",
    width: theme.spacing.unit * 50,
    backgroundColor: theme.palette.background.paper,
    boxShadow: theme.shadows[5],
    padding: theme.spacing.unit * 4
  }
});

const SimpleModal = props => {
  const { classes, open, closeModel } = props;
  console.log("open: ", open);
  return (
    <div>
      <Modal
        aria-labelledby="simple-modal-title"
        aria-describedby="simple-modal-description"
        open={open}
        onClose={closeModel}
      >
        <div style={getModalStyle()} className={classes.paper}>
          <Typography variant="h6" id="modal-title">
            Text in a modal
          </Typography>
          <Typography variant="subtitle1" id="simple-modal-description">
            success
          </Typography>
          <SimpleModalWrapped />
        </div>
      </Modal>
    </div>
  );
};

SimpleModal.propTypes = {
  classes: PropTypes.shape({ paper: PropTypes.string }).isRequired,
  open: PropTypes.bool.isRequired,
  closeModel: PropTypes.func.isRequired
};

// We need an intermediary variable for handling the recursive nesting.
const SimpleModalWrapped = withStyles(styles)(SimpleModal);

Когда я запускаю приложение и нажимаю кнопку Submit, я получаю эту ошибку:

Warning: Failed prop type: The prop `open` is marked as required in `SimpleModal`, but its value is `undefined`.
in SimpleModal (created by WithStyles(SimpleModal))
in WithStyles(SimpleModal) (created by SimpleModal)

Это ошибка из-за первого рендеринга компонента modal?Если так, как я могу это исправить?

1 Ответ

0 голосов
/ 13 декабря 2018

Похоже, что проблема вызвана рекурсивным вызовом <SimpleModalWrapped /> в строке 46 файла modal.jsx.

В этот компонент не передается реквизит, и это вызывает предупреждения PropType.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...