Skip to content

Commit 104840c

Browse files
committed
fix(compshouldupdate): fix componentshouldupdate and remove componnentwillmount hooks
1 parent ee88ee0 commit 104840c

File tree

4 files changed

+17
-17
lines changed

4 files changed

+17
-17
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-easy-state",
3-
"version": "1.0.1",
3+
"version": "1.0.3",
44
"description": "Easy State replaces React's setState with plain native JavaScript",
55
"main": "dist/easyState.js",
66
"module": "src/easyState.js",

src/easyState.js

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { observable, observe, unobserve } from '@nx-js/observer-util'
1+
import { observable, observe } from '@nx-js/observer-util'
22
import autoBind from './autoBind'
33

44
const OBSERVED_RENDER = Symbol('observed render')
@@ -27,13 +27,20 @@ export default function easyStateHOC (WrappedComp) {
2727
return result
2828
}
2929

30-
componentWillUnmount () {
31-
unobserve(this[OBSERVED_RENDER])
32-
this[OBSERVED_RENDER] = undefined
33-
super.componentWillUnmount && super.componentWillUnmount()
34-
}
30+
shouldComponentUpdate (nextProps) {
31+
const { props } = this
32+
const keys = Object.keys(props)
33+
const nextKeys = Object.keys(nextProps)
34+
35+
if (keys.length !== nextKeys.length) {
36+
return true
37+
}
3538

36-
shouldComponentUpdate () {
39+
for (let key of keys) {
40+
if (props[key] !== nextProps[key]) {
41+
return true
42+
}
43+
}
3744
return false
3845
}
3946
}

src/easyState.test.js

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,6 @@ describe('easyState', () => {
5151
expect(observer.isObservable(comp.state)).to.be.true
5252
})
5353

54-
it('should overwrite shouldComponentUpdate with an always false method', () => {
55-
const comp = new (easyState(TestComp))
56-
57-
expect(comp.shouldComponentUpdate).to.be.a('function')
58-
expect(comp.shouldComponentUpdate()).to.be.false
59-
})
60-
6154
it('should overwrite the render method', () => {
6255
const comp = new (easyState(TestComp))
6356

webpack.config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ module.exports = {
1919
use: {
2020
loader: 'babel-loader',
2121
options: {
22-
presets: ['react', 'es2015', 'stage-0'],
22+
presets: ['react', 'stage-0'],
2323
plugins: ['transform-decorators-legacy']
2424
}
2525
}
@@ -30,7 +30,7 @@ module.exports = {
3030
use: {
3131
loader: 'babel-loader',
3232
options: {
33-
presets: ['es2015', 'stage-0']
33+
presets: ['stage-0']
3434
}
3535
}
3636
}

0 commit comments

Comments
 (0)