[英]higher order method in react
我有一個React.js組件,我想像這樣定義一個更高階的方法-
updater: function (path) {
return function (value) {
this.setState(this.modules.setter.set(this.state, path, value))
}
}
其中this.modules.setter.set
返回狀態的更新版本,其新value
沿指定path
this.modules.setter.set
。
希望能夠定義這樣的方法-
updateUserStreet: this.updater('user.address.street')
我沒有創建所需的this.modules.setter.set
,但是我在使用高階方法時遇到了麻煩。 控制台告訴我this.updater is not a function
有什么想法嗎?
在React Component的.getInitialState
方法中定義updateUserStreet
,如下.getInitialState
getInitialState: function () {
this.updateUserStreet = this.updater('user.address.street');
return {...}
}
現在,react組件上定義的任何其他方法中都可以使用updateUserStreet
方法
看起來updater
實際上是React組件中的保留字。 它用於維護組件的生命周期和狀態。
我無法直接在文檔中找到它,但發現它在這里簡短提及: http : //reactkungfu.com/2016/03/dive-into-react-codebase-handling-state-changes/
嘗試將updater
重命名為其他名稱。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.