Skip to content

Commit b82773c

Browse files
authored
fix(unmount): Flush useEffect cleanup functions syncronously (#746)
1 parent 240900c commit b82773c

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

src/__tests__/render.js

+14
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,17 @@ test('renders options.wrapper around node', () => {
8787
</div>
8888
`)
8989
})
90+
91+
test('flushes useEffect cleanup functions sync on unmount()', () => {
92+
const spy = jest.fn()
93+
function Component() {
94+
React.useEffect(() => spy, [])
95+
return null
96+
}
97+
const {unmount} = render(<Component />)
98+
expect(spy).toHaveBeenCalledTimes(0)
99+
100+
unmount()
101+
102+
expect(spy).toHaveBeenCalledTimes(1)
103+
})

src/pure.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,11 @@ function render(
7474
el.forEach(e => console.log(prettyDOM(e, maxLength, options)))
7575
: // eslint-disable-next-line no-console,
7676
console.log(prettyDOM(el, maxLength, options)),
77-
unmount: () => ReactDOM.unmountComponentAtNode(container),
77+
unmount: () => {
78+
act(() => {
79+
ReactDOM.unmountComponentAtNode(container)
80+
})
81+
},
7882
rerender: rerenderUi => {
7983
render(wrapUiIfNeeded(rerenderUi), {container, baseElement})
8084
// Intentionally do not return anything to avoid unnecessarily complicating the API.

0 commit comments

Comments
 (0)