Replies: 1 comment 2 replies
-
@dmaskasky We have an issue after the upgrade to The following code works properly in import { renderHook, waitFor } from '@testing-library/react'
import { atom, useAtomValue } from 'jotai'
import { withAtomEffect } from 'jotai-effect'
const searchParamsAtom = withAtomEffect(atom({}), (_get, set) => {
// ... some irrelevant logic here ...
set(searchParamsAtom, { initial: true })
})
const myParamsAtom = withAtomEffect(atom<{ initial?: boolean }>({}), (get, set) => {
const searchParams = get(searchParamsAtom)
// ... some irrelevant logic here ...
set(myParamsAtom, searchParams)
})
it('should calculate the data', async () => {
const { result } = renderHook(() => ({
myParams: useAtomValue(myParamsAtom),
}))
await waitFor(() => {
expect(result.current).toMatchObject({ myParams: { initial: true } })
})
}) But in import { renderHook, waitFor } from '@testing-library/react'
import { atom, useAtomValue } from 'jotai'
import { withAtomEffect } from 'jotai-effect'
const searchParamsAtom = withAtomEffect(atom({}), (_get, set) => {
// ... some irrelevant logic here ...
set(searchParamsAtom, { initial: true })
})
const myParamsAtom = withAtomEffect(atom<{ initial?: boolean }>({}), (get, set) => {
// needs to be queued in 2.0.1
queueMicrotask(() => {
const searchParams = get(searchParamsAtom)
// ... some irrelevant logic here ...
set(myParamsAtom, searchParams)
})
})
it('should calculate the data', async () => {
const { result } = renderHook(() => ({
myParams: useAtomValue(myParamsAtom),
// the effect is not triggered if not explicitly mounted in 2.0.1
searchParams: useAtomValue(searchParamsAtom),
}))
await waitFor(() => {
expect(result.current).toMatchObject({ myParams: { initial: true } })
})
}) Question: Why is the microtask queueing necessary and is the effect no longer mounted when only used in an effect? (both examples work with jotai |
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.
-
I'm looking for feedback on a change that replaces Atom Effects (which rely on a microtask delay) with Synchronous Atom Effects (which run in the same task). As with Atom Effects, Synchronous Atom Effects still batch atom updates, but they eliminate the need to wait for the next microtask. They also avoid extra renders and guarantee that they run before store subscriptions (i.e.,
useAtom
,useAtomValue
, oratom.onMount
). With synchronous atom effects, you have a strong guarantee that the next value will always be correct.Atom Effect
Synchronous Atom Effect
Migration
If you still need to wait a microtask for certain use cases, you can manually queue one with queueMicrotask or
Promise.resolve().then
Beta Was this translation helpful? Give feedback.
All reactions