Skip to content

Commit 3974ece

Browse files
committed
feat (easyComp): throw on error on user defined shouldComponentUpdate
1 parent 6f72941 commit 3974ece

File tree

7 files changed

+50
-37
lines changed

7 files changed

+50
-37
lines changed

__tests__/errors.test.js

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,29 @@
1+
import { Component, PureComponent } from 'react'
12
import { easyComp, easyStore } from 'react-easy-state'
23

34
describe('errors', () => {
4-
test('easyComp should throw on non function arguments', () => {
5-
expect(() => easyComp()).toThrow('easyComp expects a component class or function as argument.')
6-
expect(() => easyComp(12)).toThrow('easyComp expects a component class or function as argument.')
7-
expect(() => easyComp(() => {})).not.toThrow()
8-
expect(() => easyComp(class {})).not.toThrow()
5+
test('easyComp should throw on non component or function arguments', () => {
6+
class Comp extends Component {}
7+
class PureComp extends PureComponent {}
8+
function FuncComp () {}
9+
10+
expect(() => easyComp()).toThrow()
11+
expect(() => easyComp(12)).toThrow()
12+
expect(() => easyComp(Comp)).not.toThrow()
13+
expect(() => easyComp(PureComp)).not.toThrow()
14+
expect(() => easyComp(FuncComp)).not.toThrow()
15+
})
16+
17+
test('easyComp should throw when shouldComponentUpdate is defined', () => {
18+
class DefinedComp extends Component {
19+
shouldComponentUpdate () {}
20+
}
21+
expect(() => easyComp(DefinedComp)).toThrow()
922
})
1023

1124
test('easyStore should throw on non object argument', () => {
12-
expect(() => easyStore()).toThrow('easyStore expects an object as argument.')
13-
expect(() => easyStore(null)).toThrow('easyStore expects an object as argument.')
25+
expect(() => easyStore()).toThrow()
26+
expect(() => easyStore(null)).toThrow()
1427
expect(() => easyStore({})).not.toThrow()
1528
})
1629
})

dist/clock.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27111,6 +27111,10 @@ function easyCompHOC(WrappedComp) {
2711127111
WrappedComp.defaultProps = renderer.defaultProps;
2711227112
}
2711327113

27114+
if (typeof WrappedComp.prototype.shouldComponentUpdate === 'function') {
27115+
throw new Error('Do not implement shouldComponentUpdate, easyState already optimizes it for you!');
27116+
}
27117+
2711427118
return function (_WrappedComp) {
2711527119
_inherits(EasyCompWrapper, _WrappedComp);
2711627120

@@ -27147,11 +27151,7 @@ function easyCompHOC(WrappedComp) {
2714727151
}
2714827152
}, {
2714927153
key: 'shouldComponentUpdate',
27150-
value: function shouldComponentUpdate(nextProps) {
27151-
if (_get(EasyCompWrapper.prototype.__proto__ || Object.getPrototypeOf(EasyCompWrapper.prototype), 'shouldComponentUpdate', this)) {
27152-
return _get(EasyCompWrapper.prototype.__proto__ || Object.getPrototypeOf(EasyCompWrapper.prototype), 'shouldComponentUpdate', this).call(this);
27153-
}
27154-
27154+
value: function shouldComponentUpdate(nextProps, nextState) {
2715527155
var props = this.props;
2715627156

2715727157
var keys = Object.keys(props);
@@ -38893,8 +38893,8 @@ function _possibleConstructorReturn(self, call) { if (!self) { throw new Referen
3889338893

3889438894
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
3889538895

38896-
var App = function (_Component) {
38897-
_inherits(App, _Component);
38896+
var App = function (_PureComponent) {
38897+
_inherits(App, _PureComponent);
3889838898

3889938899
function App() {
3890038900
_classCallCheck(this, App);
@@ -38941,7 +38941,7 @@ var App = function (_Component) {
3894138941
}]);
3894238942

3894338943
return App;
38944-
}(_react.Component);
38944+
}(_react.PureComponent);
3894538945

3894638946
// wrap the component with easyComp before exporting it
3894738947

dist/contacts.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22748,6 +22748,10 @@ function easyCompHOC(WrappedComp) {
2274822748
WrappedComp.defaultProps = renderer.defaultProps;
2274922749
}
2275022750

22751+
if (typeof WrappedComp.prototype.shouldComponentUpdate === 'function') {
22752+
throw new Error('Do not implement shouldComponentUpdate, easyState already optimizes it for you!');
22753+
}
22754+
2275122755
return function (_WrappedComp) {
2275222756
_inherits(EasyCompWrapper, _WrappedComp);
2275322757

@@ -22784,11 +22788,7 @@ function easyCompHOC(WrappedComp) {
2278422788
}
2278522789
}, {
2278622790
key: 'shouldComponentUpdate',
22787-
value: function shouldComponentUpdate(nextProps) {
22788-
if (_get(EasyCompWrapper.prototype.__proto__ || Object.getPrototypeOf(EasyCompWrapper.prototype), 'shouldComponentUpdate', this)) {
22789-
return _get(EasyCompWrapper.prototype.__proto__ || Object.getPrototypeOf(EasyCompWrapper.prototype), 'shouldComponentUpdate', this).call(this);
22790-
}
22791-
22791+
value: function shouldComponentUpdate(nextProps, nextState) {
2279222792
var props = this.props;
2279322793

2279422794
var keys = Object.keys(props);

dist/easyState.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,10 @@ function easyCompHOC (WrappedComp) {
507507
WrappedComp.defaultProps = renderer.defaultProps
508508
}
509509

510+
if (typeof WrappedComp.prototype.shouldComponentUpdate === 'function') {
511+
throw new Error('Do not implement shouldComponentUpdate, easyState already optimizes it for you!')
512+
}
513+
510514
return class EasyCompWrapper extends WrappedComp {
511515
constructor (props) {
512516
super(props)
@@ -532,11 +536,7 @@ function easyCompHOC (WrappedComp) {
532536
return this[RENDER_RESULT]
533537
}
534538

535-
shouldComponentUpdate (nextProps) {
536-
if (super.shouldComponentUpdate) {
537-
return super.shouldComponentUpdate()
538-
}
539-
539+
shouldComponentUpdate (nextProps, nextState) {
540540
const { props } = this
541541
const keys = Object.keys(props)
542542
const nextKeys = Object.keys(nextProps)

dist/todoMVC.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22748,6 +22748,10 @@ function easyCompHOC(WrappedComp) {
2274822748
WrappedComp.defaultProps = renderer.defaultProps;
2274922749
}
2275022750

22751+
if (typeof WrappedComp.prototype.shouldComponentUpdate === 'function') {
22752+
throw new Error('Do not implement shouldComponentUpdate, easyState already optimizes it for you!');
22753+
}
22754+
2275122755
return function (_WrappedComp) {
2275222756
_inherits(EasyCompWrapper, _WrappedComp);
2275322757

@@ -22784,11 +22788,7 @@ function easyCompHOC(WrappedComp) {
2278422788
}
2278522789
}, {
2278622790
key: 'shouldComponentUpdate',
22787-
value: function shouldComponentUpdate(nextProps) {
22788-
if (_get(EasyCompWrapper.prototype.__proto__ || Object.getPrototypeOf(EasyCompWrapper.prototype), 'shouldComponentUpdate', this)) {
22789-
return _get(EasyCompWrapper.prototype.__proto__ || Object.getPrototypeOf(EasyCompWrapper.prototype), 'shouldComponentUpdate', this).call(this);
22790-
}
22791-
22791+
value: function shouldComponentUpdate(nextProps, nextState) {
2279222792
var props = this.props;
2279322793

2279422794
var keys = Object.keys(props);

examples/clock/App.jsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import React, { Component } from 'react'
1+
import React, { PureComponent } from 'react'
22
import moment from 'moment'
33
import { easyComp } from 'react-easy-state'
44

5-
class App extends Component {
5+
class App extends PureComponent {
66
constructor () {
77
super()
88

src/easyComp.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ export default function easyCompHOC (WrappedComp) {
2424
WrappedComp.defaultProps = renderer.defaultProps
2525
}
2626

27+
if (typeof WrappedComp.prototype.shouldComponentUpdate === 'function') {
28+
throw new Error('Do not implement shouldComponentUpdate, easyState already optimizes it for you!')
29+
}
30+
2731
return class EasyCompWrapper extends WrappedComp {
2832
constructor (props) {
2933
super(props)
@@ -49,11 +53,7 @@ export default function easyCompHOC (WrappedComp) {
4953
return this[RENDER_RESULT]
5054
}
5155

52-
shouldComponentUpdate (nextProps) {
53-
if (super.shouldComponentUpdate) {
54-
return super.shouldComponentUpdate()
55-
}
56-
56+
shouldComponentUpdate (nextProps, nextState) {
5757
const { props } = this
5858
const keys = Object.keys(props)
5959
const nextKeys = Object.keys(nextProps)

0 commit comments

Comments
 (0)