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

Properly store owner data for cloned elements #126

Merged
merged 1 commit into from
Jul 17, 2020
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
43 changes: 30 additions & 13 deletions src/whyDidYouRender.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,27 @@ export default function whyDidYouRender(React, userOptions){

const origCreateElement = React.createElement
const origCreateFactory = React.createFactory
const origCloneElement = React.cloneElement

let componentsMap = new WeakMap()
const ownerDataMap = new WeakMap()
const hooksRef = {current: []}

function storeOwnerData(element){
const OwnerInstance = React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ReactCurrentOwner.current
if(OwnerInstance){
const Component = OwnerInstance.type.ComponentForHooksTracking || OwnerInstance.type
const displayName = getDisplayName(Component)
ownerDataMap.set(element.props, {
Component,
displayName,
props: OwnerInstance.pendingProps,
state: OwnerInstance.stateNode != null ? OwnerInstance.stateNode.state : null,
hooks: hooksRef.current
})
}
}

// Intercept assignments to ReactCurrentOwner.current and reset hooksRef
let currentOwner = null
if(React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED){
Expand Down Expand Up @@ -161,18 +177,7 @@ export default function whyDidYouRender(React, userOptions){

const element = origCreateElement.apply(React, [WDYRPatchedComponent, ...rest])
if(options.logOwnerReasons){
const OwnerInstance = React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ReactCurrentOwner.current
if(OwnerInstance){
const Component = OwnerInstance.type.ComponentForHooksTracking || OwnerInstance.type
const displayName = getDisplayName(Component)
ownerDataMap.set(element.props, {
Component,
displayName,
props: OwnerInstance.pendingProps,
state: OwnerInstance.stateNode != null ? OwnerInstance.stateNode.state : null,
hooks: hooksRef.current
})
}
storeOwnerData(element)
}

return element
Expand Down Expand Up @@ -205,6 +210,17 @@ export default function whyDidYouRender(React, userOptions){

Object.assign(React.createFactory, origCreateFactory)

React.cloneElement = (...args) => {
const element = origCloneElement.apply(React, args)
if(options.logOwnerReasons){
storeOwnerData(element)
}

return element
}

Object.assign(React.cloneElement, origCloneElement)

if(options.trackHooks){
const nativeHooks = Object.entries(hooksConfig).map(([hookName, hookTrackingConfig]) => {
return [React, hookName, hookTrackingConfig]
Expand Down Expand Up @@ -232,7 +248,8 @@ export default function whyDidYouRender(React, userOptions){
React.__REVERT_WHY_DID_YOU_RENDER__ = () => {
Object.assign(React, {
createElement: origCreateElement,
createFactory: origCreateFactory
createFactory: origCreateFactory,
cloneElement: origCloneElement
})

componentsMap = null
Expand Down
48 changes: 48 additions & 0 deletions tests/logOwnerReasons.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,19 @@ function createOwners(Child){
return {Owner, ClassOwner, HooksOwner}
}

function CloneOwner({children}){
/* eslint-disable no-unused-vars */
const [a, setA] = React.useState(1)
const [b, setB] = React.useState(1)
/* eslint-enable */
React.useEffect(() => {
setA(2)
setB(2)
}, [])

return React.cloneElement(children)
}

describe('logOwnerReasons - function child', () => {
const Child = () => null
Child.whyDidYouRender = true
Expand Down Expand Up @@ -188,6 +201,41 @@ describe('logOwnerReasons - function child', () => {
}
})
})

test('owner uses cloneElement', () => {
rtl.render(<CloneOwner><Child/></CloneOwner>)

expect(updateInfos).toHaveLength(1)
expect(updateInfos[0].reason).toEqual({
propsDifferences: [],
stateDifferences: false,
hookDifferences: false,
ownerDifferences: {
propsDifferences: false,
stateDifferences: false,
hookDifferences: [
{
hookName: 'useState',
differences: [{
pathString: '',
diffType: diffTypes.different,
prevValue: 1,
nextValue: 2
}]
},
{
hookName: 'useState',
differences: [{
pathString: '',
diffType: diffTypes.different,
prevValue: 1,
nextValue: 2
}]
}
]
}
})
})
})


Expand Down