Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
25 changes: 14 additions & 11 deletions src/react/useAtomValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,22 +102,25 @@ const createContinuablePromise = <T>(

type Options = Parameters<typeof useStore>[0] & {
delay?: number
unstable_defaultUse?: boolean
unstable_promiseStatus?: boolean
}

export function useAtomValue<Value>(
export function useAtomValue<Value, O extends Options = Options>(
atom: Atom<Value>,
options?: Options,
): Awaited<Value>

export function useAtomValue<AtomType extends Atom<unknown>>(
atom: AtomType,
options?: Options,
): Awaited<ExtractAtomValue<AtomType>>
options?: O,
): O extends { unstable_defaultUse: true } ? Awaited<Value> : Value
export function useAtomValue<Value, O extends Options = Options>(
atom: Atom<Value>,
options?: O,
): O extends { unstable_defaultUse: true } ? Awaited<Value> : Value

export function useAtomValue<Value>(atom: Atom<Value>, options?: Options) {
const { delay, unstable_promiseStatus: promiseStatus = !React.use } =
options || {}
const {
delay,
unstable_promiseStatus: promiseStatus = !React.use,
unstable_defaultUse = true,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should warn if it's undefined for the migration. So, it's too early to merge this PR.

} = options || {}
const store = useStore(options)

const [[valueFromReducer, storeFromReducer, atomFromReducer], rerender] =
Expand Down Expand Up @@ -174,7 +177,7 @@ export function useAtomValue<Value>(atom: Atom<Value>, options?: Options) {
if (promiseStatus) {
attachPromiseStatus(promise)
}
return use(promise)
return unstable_defaultUse ? use(promise) : promise
}
return value as Awaited<Value>
}
25 changes: 23 additions & 2 deletions tests/react/useAtomValue.test.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Component, StrictMode, Suspense } from 'react'
import { Component, StrictMode, Suspense, use } from 'react'
import type { ReactNode } from 'react'
import { act, render, screen } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { expect, it } from 'vitest'
import { expect, expectTypeOf, it } from 'vitest'
import { useAtomValue, useSetAtom } from 'jotai/react'
import { atom } from 'jotai/vanilla'

Expand Down Expand Up @@ -120,3 +120,24 @@

expect(await screen.findByText('obj: 1,2')).toBeInTheDocument()
})

it('useAtomValue with unstable_defaultUse', async () => {
const numberAtom = atom(Promise.resolve(1 as const))

const ObjComponent = () => {
const value = useAtomValue(numberAtom, { unstable_defaultUse: false })
expectTypeOf(value).toEqualTypeOf<Promise<1>>()

Check failure on line 130 in tests/react/useAtomValue.test.tsx

View workflow job for this annotation

GitHub Actions / test_old_typescript (3.9.7)

Untyped function calls may not accept type arguments.

Check failure on line 130 in tests/react/useAtomValue.test.tsx

View workflow job for this annotation

GitHub Actions / test_old_typescript (4.0.5)

Untyped function calls may not accept type arguments.

Check failure on line 130 in tests/react/useAtomValue.test.tsx

View workflow job for this annotation

GitHub Actions / test_old_typescript (3.8.3)

Untyped function calls may not accept type arguments.
return <div>number: {use(value)}</div>

Check failure on line 131 in tests/react/useAtomValue.test.tsx

View workflow job for this annotation

GitHub Actions / test_multiple_versions (18.3.1)

tests/react/useAtomValue.test.tsx > useAtomValue with unstable_defaultUse

TypeError: (0 , use) is not a function ❯ ObjComponent tests/react/useAtomValue.test.tsx:131:26 ❯ renderWithHooks node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom.development.js:15486:18 ❯ mountIndeterminateComponent node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom.development.js:20103:13 ❯ beginWork node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom.development.js:21626:16 ❯ beginWork$1 node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom.development.js:27465:14 ❯ performUnitOfWork node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom.development.js:26599:12 ❯ workLoopSync node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom.development.js:26505:5 ❯ renderRootSync node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom.development.js:26473:7 ❯ recoverFromConcurrentError node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom.development.js:25889:20 ❯ performSyncWorkOnRoot node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom.development.js:26135:20

Check failure on line 131 in tests/react/useAtomValue.test.tsx

View workflow job for this annotation

GitHub Actions / test_multiple_versions (18.0.0)

tests/react/useAtomValue.test.tsx > useAtomValue with unstable_defaultUse

TypeError: (0 , use) is not a function ❯ ObjComponent tests/react/useAtomValue.test.tsx:131:26 ❯ renderWithHooks node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom.development.js:16141:18 ❯ mountIndeterminateComponent node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom.development.js:20838:13 ❯ beginWork node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom.development.js:22342:16 ❯ beginWork$1 node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom.development.js:27219:14 ❯ performUnitOfWork node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom.development.js:26395:12 ❯ workLoopSync node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom.development.js:26303:5 ❯ renderRootSync node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom.development.js:26271:7 ❯ recoverFromConcurrentError node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom.development.js:25689:20 ❯ performSyncWorkOnRoot node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom.development.js:25935:20

Check failure on line 131 in tests/react/useAtomValue.test.tsx

View workflow job for this annotation

GitHub Actions / test_multiple_builds (umd, production)

tests/react/useAtomValue.test.tsx > useAtomValue with unstable_defaultUse

TypeError: (0 , use) is not a function ❯ ObjComponent tests/react/useAtomValue.test.tsx:131:26 ❯ Ch node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom.production.min.js:157:137 ❯ ck node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom.production.min.js:267:460 ❯ bk node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom.production.min.js:250:347 ❯ ak node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom.production.min.js:250:278 ❯ Tj node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom.production.min.js:250:138 ❯ Lj node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom.production.min.js:243:163 ❯ Jg node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom.production.min.js:237:175 ❯ lk node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom.production.min.js:285:27 ❯ node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom.production.min.js:289:301

Check failure on line 131 in tests/react/useAtomValue.test.tsx

View workflow job for this annotation

GitHub Actions / test_multiple_versions (18.1.0)

tests/react/useAtomValue.test.tsx > useAtomValue with unstable_defaultUse

TypeError: (0 , use) is not a function ❯ ObjComponent tests/react/useAtomValue.test.tsx:131:26 ❯ renderWithHooks node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom.development.js:16175:18 ❯ mountIndeterminateComponent node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom.development.js:20913:13 ❯ beginWork node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom.development.js:22416:16 ❯ beginWork$1 node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom.development.js:27381:14 ❯ performUnitOfWork node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom.development.js:26516:12 ❯ workLoopSync node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom.development.js:26422:5 ❯ renderRootSync node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom.development.js:26390:7 ❯ recoverFromConcurrentError node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom.development.js:25806:20 ❯ performSyncWorkOnRoot node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom.development.js:26052:20

Check failure on line 131 in tests/react/useAtomValue.test.tsx

View workflow job for this annotation

GitHub Actions / test_multiple_versions (16.14.0)

tests/react/useAtomValue.test.tsx > useAtomValue with unstable_defaultUse

TypeError: (0 , use) is not a function ❯ ObjComponent tests/react/useAtomValue.test.tsx:131:26 ❯ renderWithHooks node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom.development.js:14803:18 ❯ mountIndeterminateComponent node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom.development.js:17482:13 ❯ beginWork node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom.development.js:18596:16 ❯ HTMLUnknownElement.callCallback node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom.development.js:188:14 ❯ HTMLUnknownElement.callTheUserObjectsOperation node_modules/.pnpm/[email protected]/node_modules/jsdom/lib/jsdom/living/generated/EventListener.js:26:30 ❯ innerInvokeEventListeners node_modules/.pnpm/[email protected]/node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:350:25 ❯ invokeEventListeners node_modules/.pnpm/[email protected]/node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:286:3 ❯ HTMLUnknownElementImpl._dispatch node_modules/.pnpm/[email protected]/node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:233:9 ❯ HTMLUnknownElementImpl.dispatchEvent node_modules/.pnpm/[email protected]/node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:104:17

Check failure on line 131 in tests/react/useAtomValue.test.tsx

View workflow job for this annotation

GitHub Actions / test_multiple_versions (17.0.0)

tests/react/useAtomValue.test.tsx > useAtomValue with unstable_defaultUse

TypeError: (0 , use) is not a function ❯ ObjComponent tests/react/useAtomValue.test.tsx:131:26 ❯ renderWithHooks node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom.development.js:14977:18 ❯ mountIndeterminateComponent node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom.development.js:17803:13 ❯ beginWork node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom.development.js:19041:16 ❯ HTMLUnknownElement.callCallback node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom.development.js:3945:14 ❯ HTMLUnknownElement.callTheUserObjectsOperation node_modules/.pnpm/[email protected]/node_modules/jsdom/lib/jsdom/living/generated/EventListener.js:26:30 ❯ innerInvokeEventListeners node_modules/.pnpm/[email protected]/node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:350:25 ❯ invokeEventListeners node_modules/.pnpm/[email protected]/node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:286:3 ❯ HTMLUnknownElementImpl._dispatch node_modules/.pnpm/[email protected]/node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:233:9 ❯ HTMLUnknownElementImpl.dispatchEvent node_modules/.pnpm/[email protected]/node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:104:17

Check failure on line 131 in tests/react/useAtomValue.test.tsx

View workflow job for this annotation

GitHub Actions / test_multiple_versions (18.2.0)

tests/react/useAtomValue.test.tsx > useAtomValue with unstable_defaultUse

TypeError: (0 , use) is not a function ❯ ObjComponent tests/react/useAtomValue.test.tsx:131:26 ❯ renderWithHooks node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom.development.js:16305:18 ❯ mountIndeterminateComponent node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom.development.js:20074:13 ❯ beginWork node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom.development.js:21587:16 ❯ beginWork$1 node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom.development.js:27426:14 ❯ performUnitOfWork node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom.development.js:26560:12 ❯ workLoopSync node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom.development.js:26466:5 ❯ renderRootSync node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom.development.js:26434:7 ❯ recoverFromConcurrentError node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom.development.js:25850:20 ❯ performSyncWorkOnRoot node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom.development.js:26096:20

Check failure on line 131 in tests/react/useAtomValue.test.tsx

View workflow job for this annotation

GitHub Actions / test_multiple_builds (cjs, production)

tests/react/useAtomValue.test.tsx > useAtomValue with unstable_defaultUse

TypeError: (0 , use) is not a function ❯ ObjComponent tests/react/useAtomValue.test.tsx:131:26 ❯ Ch node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom.production.min.js:157:137 ❯ ck node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom.production.min.js:267:460 ❯ bk node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom.production.min.js:250:347 ❯ ak node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom.production.min.js:250:278 ❯ Tj node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom.production.min.js:250:138 ❯ Lj node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom.production.min.js:243:163 ❯ Jg node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom.production.min.js:237:175 ❯ lk node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom.production.min.js:285:27 ❯ node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom.production.min.js:289:301

Check failure on line 131 in tests/react/useAtomValue.test.tsx

View workflow job for this annotation

GitHub Actions / test_multiple_builds (esm, production)

tests/react/useAtomValue.test.tsx > useAtomValue with unstable_defaultUse

TypeError: (0 , use) is not a function ❯ ObjComponent tests/react/useAtomValue.test.tsx:131:26 ❯ Ch node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom.production.min.js:157:137 ❯ ck node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom.production.min.js:267:460 ❯ bk node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom.production.min.js:250:347 ❯ ak node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom.production.min.js:250:278 ❯ Tj node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom.production.min.js:250:138 ❯ Lj node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom.production.min.js:243:163 ❯ Jg node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom.production.min.js:237:175 ❯ lk node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom.production.min.js:285:27 ❯ node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom.production.min.js:289:301
}

await act(() =>
render(
<StrictMode>
<ObjComponent />
</StrictMode>,
),
)

expect(await screen.findByText('number: 1')).toBeInTheDocument()
})
Loading