ReactJS新的PropType包抛出错误

时间:2019-07-07 05:59:01

标签: reactjs react-proptypes

在React组件中,我声明了以下内容:

static propTypes = {
        data: PropTypes.shape({
            id: PropTypes.string.isRequired,
            title: PropTypes.string.isRequired,
            handle: PropTypes.string.isRequired,
            tags: PropTypes.array.isRequired,
            images: PropTypes.shape({
                edges: PropTypes.object({
                    node: PropTypes.shape({
                        src: PropTypes.string
                    })
                })
            }).isRequired
        }),
        images: PropTypes.object.isRequired,
        onClick: PropTypes.func
    }

在到达images时,构建似乎适用于所有情况,此时它说Calling PropTypes validators directly is not supported by the prop-types package. Use PropTypes.checkPropTypes() to call them.

我不理解使用静态对象作为参数与为什么引发此错误之间的关系。这也不是我的代码,所以我不太确定为什么图像道具无法展平。

1 个答案:

答案 0 :(得分:1)

您不能直接以形状调用PropTypes.object()。试试这个

images: PropTypes.shape({
     edges: PropTypes.shape({
        node: PropTypes.shape({
           src: PropTypes.string
        })
     })
}).isRequired