【问题标题】:Nothing was returned from render when using prototype使用原型时渲染没有返回任何内容
【发布时间】:2019-01-28 17:29:13
【问题描述】:
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;

当我在我的组件中使用它时,我得到没有从渲染返回任何内容? 但是当我评论这部分时

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

一切正常? 有什么帮助吗? 没有道具类型,一切都很好,或者我做错了什么。

【问题讨论】:

  • 您能展示一下您是如何使用它的吗?
  • 你应该设置的属性是propTypes而不是prototype,这是完全不同的东西。
  • 我都试过了,还是一样的错误
  • @JoeWarner 我把它修剪成只有 render() { return hi },但还是一样,当我取消注释该 propTypes 时,一切正常
  • @SarmadAijaz 是的,这是 trinx 所说的

标签: javascript reactjs react-native


【解决方案1】:

ContentLoader.propTypes 而不是ContentLoader.prototype

prototype 用于向对象添加方法,与类型检查无关。

这是一个工作示例:https://stackblitz.com/edit/react-ww1xyk

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

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

【讨论】:

    猜你喜欢
    • 2018-04-05
    • 2021-01-01
    • 2019-04-14
    • 1970-01-01
    • 1970-01-01
    • 2022-08-23
    • 2018-07-09
    • 2020-06-25
    • 1970-01-01
    相关资源
    最近更新 更多