Last active
June 2, 2017 11:13
-
-
Save typoerr/a4e7cfdac6c069aa9ba616fb1d57fd39 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| import { Component } from 'preact'; | |
| export interface Props { | |
| cond: boolean | ((prevProps: any) => boolean); | |
| [K: string]: any; | |
| } | |
| export default class ShouldChildrenUpdate extends Component<Props, void> { | |
| prevProps: any = this.props; | |
| shouldComponentUpdate(nextProps: Props) { | |
| const { cond, ...rest } = nextProps; | |
| const result = (typeof cond === 'function') ? cond(this.prevProps) : cond; | |
| this.prevProps = rest; | |
| return result; | |
| } | |
| render(props: any) { | |
| return props.children && props.children[0] || null; | |
| } | |
| } |
Author
Author
注意事項
<ShouldChildrenUpdate {...this.state} cond={shouldUpdate.bind(this)} >
<Child count={this.state.count}>
{[1, 2, 3].map(x => <div>{x}</div>)} // (1)
</Child>
</ShouldChildrenUpdate>(1)の計算は親コンポーネントスコープなので、updateは走らないが計算は親のrender毎に実行される
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
example