Skip to content

Commit

Permalink
fix(uncontrollable): Migrate unsafe lifecycle
Browse files Browse the repository at this point in the history
Fixes jquense#32.
  • Loading branch information
nortonwong authored Sep 20, 2019
1 parent df2cbdc commit 9ada8c1
Showing 1 changed file with 9 additions and 12 deletions.
21 changes: 9 additions & 12 deletions src/uncontrollable.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,33 +45,30 @@ export default function uncontrollable(Component, controlledValues, methods = []
this.attachRef = ref => {
this.inner = ref
}
}

shouldComponentUpdate() {
//let the forceUpdate trigger the update
return !this._notifying
}

componentWillMount() {
let props = this.props

this._values = Object.create(null)

let props = this.props
controlledProps.forEach(key => {
this._values[key] = props[Utils.defaultKey(key)]
})
}

componentWillReceiveProps(nextProps) {
shouldComponentUpdate() {
//let the forceUpdate trigger the update
return !this._notifying
}

componentDidUpdate(prevProps) {
let props = this.props

controlledProps.forEach(key => {
/**
* If a prop switches from controlled to Uncontrolled
* reset its value to the defaultValue
*/
if (!Utils.isProp(nextProps, key) && Utils.isProp(props, key)) {
this._values[key] = nextProps[Utils.defaultKey(key)]
if (!Utils.isProp(props, key) && Utils.isProp(prevProps, key)) {
this._values[key] = props[Utils.defaultKey(key)]
}
})
}
Expand Down

0 comments on commit 9ada8c1

Please sign in to comment.