失败的道具类型 - 值是'undefined`

时间:2017-09-09 06:41:15

标签: reactjs

我看过其他类似的问题,但似乎无法找到答案......

我收到错误:

  

警告:道具类型失败:道具heightAudioPlayer标记为必需,但其值为undefined

尝试以下代码时:

import React, { Component } from "react";
import { Row, Col } from "react-bootstrap";
import Audio from "react-audioplayer";
import "./../../Assets/Css/AudioPlayer.css";
import PropTypes from "prop-types";

export default class AudioPlayer extends Component {
    render(props) {
        const playlist = this.props.playlist;

        const AudioStyles = {
            width: "99%",
            margin: "1% 0"
        };

        const AudioProps = {
            height: 600,
            style: AudioStyles,
            playlist: playlist,
            fullPlayer: true
        };

        return (
            <Row>
                <Col xs={12}>
                    <Audio {...AudioProps} />
                </Col>
            </Row>
        );
    }
}

AudioPlayer.propTypes = {
    height: PropTypes.number.isRequired
};

代码仍按预期运行,但会出现上述错误。

我应该以不同方式检查道具类型吗?

1 个答案:

答案 0 :(得分:2)

根据我在您的问题描述中看到的内容,您prop height组件中不依赖AudioPlayer。您希望在Audio组件中使用它,因此您需要为Proptype

指定Audio component

所以在音频组件文件中添加和删除AudioPlayer组件文件。

Audio.propTypes = {
    height: PropTypes.number.isRequired
};