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

Proper children reconcile for nested tags #871

Merged
merged 1 commit into from
Feb 25, 2018
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/reconciler/hotReplacementRender.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,17 +129,19 @@ const mapChildren = (children, instances) => ({
}
}

const newChildren = asArray((child.props && child.props.children) || [])
const newChildren = asArray(
(child.props && child.props.children) || child.children || [],
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When result of this function on next render iteration got in here again - it will be just ignored.
That was the first problem.

)
const nextChildren =
oldChildren.length && mapChildren(newChildren, oldChildren)

return {
...instanceLine,
// actually child merge is needed only for TAGs, and usually don't work for Components.
// actually child merge is needed only for "HTML TAG"s, and usually don't work for Components.
// the children from an instance or rendered children
// while children from a props is just a props.
// they could not exists. they could differ.
...(nextChildren ? { children: nextChildren } : {}),
...(nextChildren || {}),
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was a second. A little "gap" in the tree

type: child.type,
}
}),
Expand Down
42 changes: 40 additions & 2 deletions test/reconciler.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,12 +213,50 @@ describe('reconciler', () => {

incrementGeneration()
wrapper.setProps({ update: 'now' })
expect(First.rendered).toHaveBeenCalledTimes(4)
expect(First.rendered).toHaveBeenCalledTimes(3)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This means - fixed!

expect(Second.rendered).toHaveBeenCalledTimes(3)

incrementGeneration()
wrapper.setProps({ second: false })
expect(First.rendered).toHaveBeenCalledTimes(5)
expect(Second.rendered).toHaveBeenCalledTimes(4)

expect(First.unmounted).toHaveBeenCalledTimes(0)
expect(Second.unmounted).toHaveBeenCalledTimes(1)
})

it('should use new children branch during reconcile for full components', () => {
const First = spyComponent(() => <u>1</u>, 'test', '1')
const Second = spyComponent(() => <u>2</u>, 'test', '2')

const Section = ({ children }) => <div>{children}</div>

const App = ({ second }) => (
<div>
<div>
<Section>
<First.Component />
{second && <Second.Component />}
</Section>
</div>
</div>
)

const Mounter = ({ second }) => <App second={second} />

const wrapper = mount(<Mounter second />)

expect(First.rendered).toHaveBeenCalledTimes(1)
expect(Second.rendered).toHaveBeenCalledTimes(1)

incrementGeneration()
wrapper.setProps({ update: 'now' })
expect(First.rendered).toHaveBeenCalledTimes(3)
expect(Second.rendered).toHaveBeenCalledTimes(3)

incrementGeneration()
wrapper.setProps({ second: false })
expect(First.rendered).toHaveBeenCalledTimes(7)
expect(First.rendered).toHaveBeenCalledTimes(5)
expect(Second.rendered).toHaveBeenCalledTimes(4)

expect(First.unmounted).toHaveBeenCalledTimes(0)
Expand Down