-
-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement shouldUpdate() and pure()
- Loading branch information
Showing
10 changed files
with
109 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,4 +15,4 @@ export class Example1 extends Component { | |
</ShouldUpdate> | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import {Component, createElement as h} from 'react'; | ||
import {shouldUpdate} from '..'; | ||
|
||
const Print = ({cnt}) => <span>Click me ({cnt})</span>; | ||
const LazyPrint = shouldUpdate((props) => props.cnt > 3)(Print); | ||
|
||
export class Example2 extends Component { | ||
state = { | ||
cnt: 0 | ||
}; | ||
|
||
render () { | ||
return ( | ||
<div onClick={() => this.setState({cnt: this.state.cnt + 1})}> | ||
<LazyPrint cnt={this.state.cnt} /> | ||
</div> | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import {Component, createElement as h} from 'react'; | ||
import {shouldUpdate} from '..'; | ||
|
||
const Print = ({cnt}) => <span>Click me ({cnt})</span>; | ||
const LazyPrint = shouldUpdate((props) => !(props.cnt % 3))(Print); | ||
|
||
export class Example3 extends Component { | ||
state = { | ||
cnt: 0 | ||
}; | ||
|
||
render () { | ||
return ( | ||
<div onClick={() => this.setState({cnt: this.state.cnt + 1})}> | ||
<LazyPrint cnt={this.state.cnt} /> | ||
</div> | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import {Component, createElement as h} from 'react'; | ||
import {pure} from '../../pure'; | ||
|
||
const Print = ({cnt}) => { | ||
console.log('RENDERING'); | ||
|
||
return <span>Click me ({cnt})</span>; | ||
}; | ||
const LazyPrint = pure(Print); | ||
|
||
export class Example4 extends Component { | ||
state = { | ||
cnt: 0 | ||
}; | ||
|
||
render () { | ||
return ( | ||
<div onClick={() => this.setState({cnt: this.state.cnt + 1})}> | ||
<LazyPrint cnt={Math.floor(this.state.cnt / 3) * 3} /> | ||
</div> | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import {equal} from 'fast-shallow-equal'; | ||
import {shouldUpdate} from './ShouldUpdate'; | ||
|
||
export const pure = shouldUpdate((a, b) => !equal(a, b)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
const keyList = Object.keys; | ||
const hasProp = Object.prototype.hasOwnProperty; | ||
|
||
const shallowEqual: (a, b) => boolean = (a, b) => { | ||
if (a === b) return true; | ||
if (!a || !b) return false; | ||
if (!(a instanceof Object) || !(b instanceof Object)) return false; | ||
|
||
const keys = keyList(a); | ||
const length = keys.length; | ||
|
||
for (let i = 0; i < length; i++) | ||
if (!(keys[i] in b)) return false; | ||
|
||
for (let i = 0; i < length; i++) | ||
if (a[keys[i]] !== b[keys[i]]) return false; | ||
|
||
return length === keyList(b).length; | ||
} | ||
|
||
export default |