Replies: 2 comments 6 replies
-
Probably, you may need to use unstable_batchedUpdates with React 16. |
Beta Was this translation helpful? Give feedback.
4 replies
-
When I tested this with React 16, here's what I got.
Test code: import React, { useEffect } from 'react'
import { atom, useAtomValue, useSetAtom } from 'jotai'
import { render, act } from '@testing-library/react'
it.only('should render once on atom update (multiple subscribers)', () => {
const focusAtom = atom(0)
const Component = jest.fn(() => {
const setIsFocused = useSetAtom(focusAtom)
const isFocused2 = useAtomValue(focusAtom);
const isFocused3 = useAtomValue(focusAtom);
const isFocused4 = useAtomValue(focusAtom);
const isFocused5 = useAtomValue(focusAtom);
const isFocused6 = useAtomValue(focusAtom);
useEffect(() => {
const handleClick = () => {
setIsFocused((v) => v + 1)
}
window.addEventListener('click', handleClick)
return () => {
window.removeEventListener('click', handleClick)
}
}, [setIsFocused])
console.log('Component rendered')
return (
<div>
<p>test</p>
</div>
)
})
render(<Component />)
expect(Component).toHaveBeenCalledTimes(1)
Component.mockClear()
act(() => {
window.dispatchEvent(new Event('click'))
})
expect(Component).toHaveBeenCalledTimes(1)
Component.mockClear()
act(() => {
window.dispatchEvent(new Event('click'))
})
expect(Component).toHaveBeenCalledTimes(1)
}) |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
The code example is as follows,First of all, the react version must be 16.x to reproduce, and I found a very strange phenomenon. If it is triggered by a react event, the component will only render once. If it is triggered by an event listener, the component will render multiple times. I suspect it is related to the logic of react's bailout, but I'm not sure. I hope you can help me solve it.
Beta Was this translation helpful? Give feedback.
All reactions