Skip to content

atomWithQuery depending on atomWithQuery atom doesn't work well #17

Open
@jet2jet

Description

@jet2jet

Description

When using atomWithQuery with an atom created with atomWithQuery (and loadable) (*), it doesn't seem to work properly.
(*) using the atom in getVariables and getPause

Reproduction

  1. Clone https://github.com/jet2jet/jotai-urql-test and npm install
  2. Run npm run start
  3. Open http://localhost:3000 , and click Detailed button
  4. Click Normal button and Detailed button for some times, and detailed list will stay loading state

Others

I wonder the following block creates an atom inside the atom getter function.
https://github.com/jotaijs/jotai-urql/blob/v0.7.1/src/common.ts#L43-L64
If I modify the code like followings, it seems to work properly. (Is it OK to use atom generators inside the atom getter function?)

Modified code
  const observableAtom = atom((get) => {
    const args = getArgs(get)
    const client = getClient(get)
    const source = getPause(get) ? null : execute(client, args)
    if (!source) {
      return null
    }
    return pipe(source, toObservable)
  })
  // Note: if the function for `initialValue` accepts `get`, these atoms could be combined into one atom
  const resultAtomWithSuspense = atomWithObservable<Result>(
    (get) => get(observableAtom),
    {}
  )
  const resultAtomWithoutSuspense = atomWithObservable<Result>(
    (get) => get(observableAtom),
    { initialValue: urqlReactCompatibleInitialState }
  )
  const baseStatusAtom = atom((get) => {
    if (!get(observableAtom)) return initialLoadAtom
    // Enables or disables suspense based off global suspense atom
    const resultAtom = get(suspenseAtom) ? resultAtomWithSuspense : resultAtomWithoutSuspense
    if (process.env.NODE_ENV !== 'production') {
      resultAtom.debugPrivate = true
    }
    return resultAtom
  })

# FYI: jotai's atomWithObservable also has calling an atom generator function inside the atom getter function:
https://github.com/pmndrs/jotai/blob/v2.4.3/src/vanilla/utils/atomWithObservable.ts#L137

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions