美文网首页React Native
React-Native 组件的属性

React-Native 组件的属性

作者: anddygon | 来源:发表于2017-04-16 17:28 被阅读6次

    1. props

    this.props contains the props that were defined by the caller of this component. See Components and Props for an introduction to props.
    In particular,this.props.children is a special prop, typically defined by the child tags in the JSX expression rather than in the tag itself.

    也就是说Props应该是该组件的调用者使用的。


    2. state

    The state contains data specific to this component that may change over time. The state is user-defined, and it should be a plain JavaScript object.
    If you don't use it inrender(), it shouldn't be on the state. For example, you can put timer IDs directly on the instance.

    state包含的数据应该都是用到render()里面的,如果不是用到render(),你可以考虑定义为props(需要外部调用者传进来)或者该组件的一个普通属性(例如组件内部用到的定时器等等)


    3 自定义的属性

    组件用到的一些对象,不需要外部传入切不参与渲染,那么就可以定义为普通的组件属性。
    例如组件用到了定时器

        setupInterval() {
            this.interval= setInterval(() => {
                   //some stuff
            }, 5000)
        }
    

    相关文章

      网友评论

        本文标题:React-Native 组件的属性

        本文链接:https://www.haomeiwen.com/subject/qeiaattx.html