ReactJS 失败的 Prop 类型,值为 'undefined'

标签 reactjs validation types

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

我收到错误:

Warning: Failed prop type: The prop height is marked as required in AudioPlayer, but its value is 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
};

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

我应该以不同的方式检查 Prop 类型吗?

最佳答案

从我在您的问题描述中看到的情况来看,您在 AudioPlayer 组件中不依赖于 prop height 。您希望将其放在 Audio 组件中,因此您需要为 Audio 组件 指定 Proptype

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

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

关于ReactJS 失败的 Prop 类型,值为 'undefined',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46127903/

相关文章:

javascript - (react,antd,card) 如何使用 antd 卡组件阻止操作按钮的 onclick 功能?

php - PHP验证Paypal Webhook签名

javascript - 使用 react 形式以 Angular 验证密码和确认密码

c++ - 我可以使自己的自定义数据类型比 C++ 中的数据类型更大吗?

javascript - 为什么会出现 SyntaxError : Assigning to rvalue?

reactjs - 甲板.gl GeoJsonLayer : update feature color on click

javascript - Yii 比较规则没有 trim

C# 泛型和类型检查

c++ - 10^10 - 10^11 范围内的数字的数据类型应该是什么?

javascript - 未能加载响应数据请求内容已从检查器缓存中逐出