Skip to content
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated: Support React-Hot-Loader compatibility #1168

Merged
merged 2 commits into from Jan 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -10,6 +10,7 @@ test/**/lcov.info
test/**/lcov-report
test/react/*/test/**/*.spec.js
test/react/**/src
test/jest-config.json
lcov.info

lib/core/metadata.js
Expand Down
52 changes: 41 additions & 11 deletions src/components/connectAdvanced.js
Expand Up @@ -120,7 +120,6 @@ export default function connectAdvanced(
const { pure } = connectOptions

let OuterBaseComponent = Component
let FinalWrappedComponent = WrappedComponent

if (pure) {
OuterBaseComponent = PureComponent
Expand All @@ -131,15 +130,25 @@ export default function connectAdvanced(
let lastState
let lastDerivedProps
let lastStore
let lastSelectorFactoryOptions
let sourceSelector

return function selectDerivedProps(state, props, store) {
return function selectDerivedProps(
state,
props,
store,
selectorFactoryOptions
) {
if (pure && lastProps === props && lastState === state) {
return lastDerivedProps
}

if (store !== lastStore) {
if (
store !== lastStore ||
lastSelectorFactoryOptions !== selectorFactoryOptions
) {
lastStore = store
lastSelectorFactoryOptions = selectorFactoryOptions
sourceSelector = selectorFactory(
store.dispatch,
selectorFactoryOptions
Expand All @@ -157,14 +166,23 @@ export default function connectAdvanced(
}

function makeChildElementSelector() {
let lastChildProps, lastForwardRef, lastChildElement
let lastChildProps, lastForwardRef, lastChildElement, lastComponent

return function selectChildElement(childProps, forwardRef) {
if (childProps !== lastChildProps || forwardRef !== lastForwardRef) {
return function selectChildElement(
WrappedComponent,
childProps,
forwardRef
) {
if (
childProps !== lastChildProps ||
forwardRef !== lastForwardRef ||
lastComponent !== WrappedComponent
) {
lastChildProps = childProps
lastForwardRef = forwardRef
lastComponent = WrappedComponent
lastChildElement = (
<FinalWrappedComponent {...childProps} ref={forwardRef} />
<WrappedComponent {...childProps} ref={forwardRef} />
)
}

Expand All @@ -182,7 +200,14 @@ export default function connectAdvanced(
)
this.selectDerivedProps = makeDerivedPropsSelector()
this.selectChildElement = makeChildElementSelector()
this.renderWrappedComponent = this.renderWrappedComponent.bind(this)
this.indirectRenderWrappedComponent = this.indirectRenderWrappedComponent.bind(
this
)
}

indirectRenderWrappedComponent(value) {
// calling renderWrappedComponent on prototype from indirectRenderWrappedComponent bound to `this`
return this.renderWrappedComponent(value)
}

renderWrappedComponent(value) {
Expand All @@ -206,10 +231,15 @@ export default function connectAdvanced(
let derivedProps = this.selectDerivedProps(
storeState,
wrapperProps,
store
store,
selectorFactoryOptions
)

return this.selectChildElement(derivedProps, forwardedRef)
return this.selectChildElement(
WrappedComponent,
derivedProps,
forwardedRef
)
}

render() {
Expand All @@ -222,7 +252,7 @@ export default function connectAdvanced(

return (
<ContextToUse.Consumer>
{this.renderWrappedComponent}
{this.indirectRenderWrappedComponent}
</ContextToUse.Consumer>
)
}
Expand Down
10 changes: 9 additions & 1 deletion test/run-tests.js
@@ -1,4 +1,6 @@
const npmRun = require('npm-run')
const fs = require('fs')
const path = require('path')
const LATEST_VERSION = '16.6'
const version = process.env.REACT || LATEST_VERSION

Expand Down Expand Up @@ -27,7 +29,13 @@ if (version.toLowerCase() === 'all') {
}
}

const configFilePath = path.join(__dirname, 'jest-config.json')

fs.writeFileSync(configFilePath, JSON.stringify(jestConfig))

const commandLine = `jest -c "${configFilePath}" ${process.argv.slice(2).join(' ')}`

npmRun.execSync(
`jest -c '${JSON.stringify(jestConfig)}' ${process.argv.slice(2).join(' ')}`,
commandLine,
{ stdio: 'inherit' }
)