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)
    }
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容