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

fix: flush pending hooks effects in rerender #32

Merged
merged 2 commits into from
May 19, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
18 changes: 18 additions & 0 deletions src/__tests__/rerender.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import '@testing-library/jest-dom/extend-expect'
import { h } from 'preact'
import { useState, useEffect } from 'preact/hooks'
import { render } from '..'

test('rerender will re-render the element', () => {
Expand All @@ -12,6 +13,23 @@ test('rerender will re-render the element', () => {
expect(container.firstChild).toHaveTextContent('hey')
})

test('rerender will flush pending hooks effects', async () => {
const Component = () => {
const [value, setValue] = useState(0)
useEffect(() => {
const timeoutId = setTimeout(() => setValue(1), 0)
return () => clearTimeout(timeoutId)
})

return value
}

const { rerender, findByText } = render(<Component />)
rerender(<Component />)

await findByText('1')
})

test('hydrate will not update props until next render', () => {
const initialInputElement = document.createElement('input')
const container = document.createElement('div')
Expand Down
2 changes: 1 addition & 1 deletion src/pure.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ function render (
: console.log(prettyDOM(el, maxLength, options)),
unmount: () => preactRender(null, container),
rerender: (rerenderUi) => {
setupRerender()()
act(() => {})
render(wrapUiIfNeeded(rerenderUi), { container, baseElement })
// Intentionally do not return anything to avoid unnecessarily complicating the API.
// folks can use all the same utilities we return in the first place that are bound to
Expand Down