refactoring: Migrate to new lifecycle methods, close #288#291
Conversation
|
有种把 vue 里面 watch 改为 computed 的感觉... |
Codecov Report
@@ Coverage Diff @@
## master #291 +/- ##
==========================================
- Coverage 99.66% 99.64% -0.02%
==========================================
Files 8 8
Lines 297 285 -12
Branches 76 74 -2
==========================================
- Hits 296 284 -12
Misses 1 1
Continue to review full report at Codecov.
|
| componentWillMount() { | ||
| this.lastInputValue = this.props.inputValue; | ||
| constructor(props) { | ||
| super(); |
| open, | ||
| optionsInfo, | ||
| // mark defaultValue has been setted, propr.defaultValue only work at the first time. | ||
| canUseDefaultValue: true, |
There was a problem hiding this comment.
this.state = {
value: props.defaultValue
}就不需要 canUseDefaultValue 了吧。
There was a problem hiding this comment.
getDerivedStateFromProps 这个方法在初始化和 props 变化的时候都会被调用,为了把赋值的相关逻辑都统一放到那里面去处理,所以加了 canUseDefaultValue。理论上也是可以把 defaultValue 相关的逻辑放到 constructor 里面的,但是有的逻辑(主要是 inputValue 初始化的逻辑)就需要在 constructor 和 getDerivedStateFromProps 里面都写一下了。
There was a problem hiding this comment.
在 constructor 里用 defaultValue 来初始化 value,然后后面都是用 prevState.value吧,value = 'value' in nextProps.value ? nextProps.value : prevState.value 这样。
ant-design/ant-design#9792