Skip to content

Commit 0438ff3

Browse files
committed
feat: add unstable_defaultUse
1 parent 53bbf93 commit 0438ff3

File tree

2 files changed

+37
-13
lines changed

2 files changed

+37
-13
lines changed

src/react/useAtomValue.ts

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -102,22 +102,25 @@ const createContinuablePromise = <T>(
102102

103103
type Options = Parameters<typeof useStore>[0] & {
104104
delay?: number
105+
unstable_defaultUse?: boolean
105106
unstable_promiseStatus?: boolean
106107
}
107108

108-
export function useAtomValue<Value>(
109+
export function useAtomValue<Value, O extends Options = Options>(
109110
atom: Atom<Value>,
110-
options?: Options,
111-
): Awaited<Value>
112-
113-
export function useAtomValue<AtomType extends Atom<unknown>>(
114-
atom: AtomType,
115-
options?: Options,
116-
): Awaited<ExtractAtomValue<AtomType>>
111+
options?: O,
112+
): O extends { unstable_defaultUse: true } ? Awaited<Value> : Value
113+
export function useAtomValue<Value, O extends Options = Options>(
114+
atom: Atom<Value>,
115+
options?: O,
116+
): O extends { unstable_defaultUse: true } ? Awaited<Value> : Value
117117

118118
export function useAtomValue<Value>(atom: Atom<Value>, options?: Options) {
119-
const { delay, unstable_promiseStatus: promiseStatus = !React.use } =
120-
options || {}
119+
const {
120+
delay,
121+
unstable_promiseStatus: promiseStatus = !React.use,
122+
unstable_defaultUse = true,
123+
} = options || {}
121124
const store = useStore(options)
122125

123126
const [[valueFromReducer, storeFromReducer, atomFromReducer], rerender] =
@@ -174,7 +177,7 @@ export function useAtomValue<Value>(atom: Atom<Value>, options?: Options) {
174177
if (promiseStatus) {
175178
attachPromiseStatus(promise)
176179
}
177-
return use(promise)
180+
return unstable_defaultUse ? use(promise) : promise
178181
}
179182
return value as Awaited<Value>
180183
}

tests/react/useAtomValue.test.tsx

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { Component, StrictMode, Suspense } from 'react'
1+
import { Component, StrictMode, Suspense, use } from 'react'
22
import type { ReactNode } from 'react'
33
import { act, render, screen } from '@testing-library/react'
44
import userEvent from '@testing-library/user-event'
5-
import { expect, it } from 'vitest'
5+
import { expect, expectTypeOf, it } from 'vitest'
66
import { useAtomValue, useSetAtom } from 'jotai/react'
77
import { atom } from 'jotai/vanilla'
88

@@ -120,3 +120,24 @@ it('useAtomValue with atom returning object', async () => {
120120

121121
expect(await screen.findByText('obj: 1,2')).toBeInTheDocument()
122122
})
123+
124+
it('useAtomValue with unstable_defaultUse', async () => {
125+
const numberAtom = atom(Promise.resolve(1 as const))
126+
127+
const ObjComponent = () => {
128+
const value = useAtomValue(numberAtom, { unstable_defaultUse: false })
129+
expectTypeOf(value).toEqualTypeOf<Promise<1>>()
130+
131+
return <div>number: {use(value)}</div>
132+
}
133+
134+
await act(() =>
135+
render(
136+
<StrictMode>
137+
<ObjComponent />
138+
</StrictMode>,
139+
),
140+
)
141+
142+
expect(await screen.findByText('number: 1')).toBeInTheDocument()
143+
})

0 commit comments

Comments
 (0)