使用 PropTypes 进行类型检查会给我一个错误,即使道具显示正确

Typechecking with PropTypes gives me an error even though the props show properly

我正在使用 prop-types 对我的组件进行类型检查,但即使我以正确的类型正确获取值,我仍然收到如下错误消息:

Warning: Failed prop type: The prop id is marked as required in NewChannel, but its value is undefined.

const NewChannel = props => {    
    const { id } = props.match.params
    return(
    //some logic
    )
}

NewChannel.propTypes = {
    id: PropTypes.string.isRequired
}

export default React.memo(NewChannel)

当我 console.log 道具 id 时,它不是未定义的。

你的类型检查说:props 将有一个 id 字段,如:props.id。但是您正在从 props.match.params 获取 id。这就是它抱怨的原因。