React-Native 组件的属性

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)
    }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容