Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Bail out if stateProps can be calculated early and did not change#348

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
gaearon merged 1 commit intomasterfrombailout
Apr 12, 2016
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletionssrc/components/connect.js
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -219,6 +219,7 @@ export default function connect(mapStateToProps, mapDispatchToProps, mergeProps,
this.mergedProps = null
this.haveOwnPropsChanged = true
this.hasStoreStateChanged = true
this.haveStatePropsBeenPrecalculated = false
this.renderedElement = null
this.finalMapDispatchToProps = null
this.finalMapStateToProps = null
Expand All@@ -229,13 +230,21 @@ export default function connect(mapStateToProps, mapDispatchToProps, mergeProps,
return
}

const prevStoreState = this.state.storeState
const storeState = this.store.getState()
const prevStoreState = this.state.storeState
if (pure && prevStoreState === storeState) {
return
}

if (!pure || prevStoreState !== storeState) {
this.hasStoreStateChanged = true
this.setState({ storeState })
if (pure && !this.doStatePropsDependOnOwnProps) {
if (!this.updateStatePropsIfNeeded()) {
return
}
this.haveStatePropsBeenPrecalculated = true
}

this.hasStoreStateChanged = true
this.setState({ storeState })
}

getWrappedInstance() {
Expand All@@ -251,11 +260,13 @@ export default function connect(mapStateToProps, mapDispatchToProps, mergeProps,
const {
haveOwnPropsChanged,
hasStoreStateChanged,
haveStatePropsBeenPrecalculated,
renderedElement
} = this

this.haveOwnPropsChanged = false
this.hasStoreStateChanged = false
this.haveStatePropsBeenPrecalculated = false

let shouldUpdateStateProps = true
let shouldUpdateDispatchProps = true
Expand All@@ -269,7 +280,9 @@ export default function connect(mapStateToProps, mapDispatchToProps, mergeProps,

let haveStatePropsChanged = false
let haveDispatchPropsChanged = false
if (shouldUpdateStateProps) {
if (haveStatePropsBeenPrecalculated) {
haveStatePropsChanged = true
} else if (shouldUpdateStateProps) {
haveStatePropsChanged = this.updateStatePropsIfNeeded()
}
if (shouldUpdateDispatchProps) {
Expand Down
46 changes: 45 additions & 1 deletiontest/components/connect.spec.js
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -1082,7 +1082,6 @@ describe('React', () => {
)
spy.destroy()


spy = expect.spyOn(console, 'error')
TestUtils.renderIntoDocument(
<ProviderMock store={store}>
Expand DownExpand Up@@ -1547,6 +1546,51 @@ describe('React', () => {
expect(renderCalls).toBe(1)
})

it('should bail out early if mapState does not depend on props', () => {
const store = createStore(stringBuilder)
let renderCalls = 0
let mapStateCalls = 0

@connect(state => {
mapStateCalls++
return state === 'aaa' ? { change: 1 } : {}
})
class Container extends Component {
render() {
renderCalls++
return <Passthrough {...this.props} />
}
}

TestUtils.renderIntoDocument(
<ProviderMock store={store}>
<Container />
</ProviderMock>
)

expect(renderCalls).toBe(1)
expect(mapStateCalls).toBe(1)

const spy = expect.spyOn(Container.prototype, 'setState').andCallThrough()

store.dispatch({ type: 'APPEND', body: 'a' })
expect(mapStateCalls).toBe(2)
expect(renderCalls).toBe(1)
expect(spy.calls.length).toBe(0)

store.dispatch({ type: 'APPEND', body: 'a' })
expect(mapStateCalls).toBe(3)
expect(renderCalls).toBe(1)
expect(spy.calls.length).toBe(0)

store.dispatch({ type: 'APPEND', body: 'a' })
expect(mapStateCalls).toBe(4)
expect(renderCalls).toBe(2)
expect(spy.calls.length).toBe(1)

spy.destroy()
})

it('should allow providing a factory function to mapStateToProps', () => {
let updatedCount = 0
let memoizedReturnCount = 0
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp