简体   繁体   English

使用原型时渲染没有返回任何内容

[英]Nothing was returned from render when using prototype

class ContentLoader extends Component {

state = {
    animation: new Animated.Value(0)
}


render() {
    if (this.props.loading) {
        return (<View
            style={{
                height: 100,
                width: 100
            }}
        />);
    }
    return this.props.children;

}
}


ContentLoader.prototype = {
    primaryColor: PropTypes.string,
    secondaryColor: PropTypes.string,
    animationDuration: PropTypes.number,
    // children: PropTypes.element.isRequired,
    style: PropTypes.object.isRequired,
    loading: PropTypes.bool,

}
ContentLoader.defaultProps = {
    primaryColor: 'rgba(195, 191, 191, 1)',
    secondaryColor: 'rgba(218, 215, 215, 1)',
    animationDuration: 500,
    loading: true
};

export default ContentLoader;

When i use this in my component, I get Nothing was returned from render?当我在我的组件中使用它时,渲染没有返回任何内容? but when i comment this part但是当我评论这部分时

 ContentLoader.prototype = {
    primaryColor: PropTypes.string,
    secondaryColor: PropTypes.string,
    animationDuration: PropTypes.number,
    // children: PropTypes.element.isRequired,
    style: PropTypes.object.isRequired,
    loading: PropTypes.bool,
}

Everything works?一切正常吗? Any help?有什么帮助吗? Everthing works well without prop types, Or maybe I am doing something wrong.没有道具类型,一切都很好,或者我做错了什么。

ContentLoader.propTypes instead of ContentLoader.prototype ContentLoader.propTypes而不是ContentLoader.prototype

prototype is used for adding methods to a object and isn't related to type checking.原型用于向对象添加方法,与类型检查无关。

here is a working example: https://stackblitz.com/edit/react-ww1xyk这是一个工作示例: https : //stackblitz.com/edit/react-ww1xyk

https://reactjs.org/docs/typechecking-with-proptypes.html https://reactjs.org/docs/typechecking-with-proptypes.html

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/prototype https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/prototype

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM