-
-
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.
- Loading branch information
Showing
5 changed files
with
58 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import {Component, createElement as h} from 'react'; | ||
import {ShouldUpdate} from '..'; | ||
|
||
export class Example1 extends Component { | ||
state = { | ||
cnt: 0 | ||
}; | ||
|
||
render () { | ||
return ( | ||
<ShouldUpdate when={(props) => props.cnt > 3} props={this.state}> | ||
<div onClick={() => this.setState({cnt: this.state.cnt + 1})}> | ||
Click me ({this.state.cnt}) | ||
</div> | ||
</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,16 @@ | ||
import {Component, createElement as h} from 'react'; | ||
import {storiesOf} from '@storybook/react'; | ||
import {action} from '@storybook/addon-actions'; | ||
import {linkTo} from '@storybook/addon-links'; | ||
import {ShouldUpdate} from '..'; | ||
import ShowDocs from '../../../.storybook/ShowDocs' | ||
import {Example1} from './Example1'; | ||
|
||
storiesOf('Inversion/ShouldUpdate', module) | ||
// .add('Documentation', () => h(ShowDocs, {md: require('../../../docs/en/ShouldUpdate.md')})) | ||
.add('Renders children', () => | ||
h(ShouldUpdate, {when: () => false, props: {}}, | ||
h('div', {}, 'Hello foobar!') | ||
) | ||
) | ||
.add('When greater than 3', () => <Example1 />) |
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,20 @@ | ||
import {Component} from 'react'; | ||
import renderProp from '../util/renderProp'; | ||
|
||
export interface IShouldUpdateProps { | ||
props; | ||
when: (newProps, oldProps) => boolean; | ||
} | ||
|
||
export interface IShouldUpdateState { | ||
} | ||
|
||
export class ShouldUpdate extends Component<IShouldUpdateProps, IShouldUpdateState> { | ||
shouldComponentUpdate (props) { | ||
return this.props.when(props.props, this.props.props); | ||
} | ||
|
||
render () { | ||
return renderProp(this.props, this.props.props); | ||
} | ||
} |
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