Skip to content

[Question] Why does the fetcher store value become {loading: false} when there are no listeners ? #62

@hamza0867

Description

@hamza0867

Hi, thank you for the amazing work that is nanostoes and nanoquey. I have a question:

In the following example

import {useState, useEffect} from 'preact/hooks'
import {nanoquery} from '@nanostores/query';
import {FunctionComponent} from 'preact';


const [createFetcherStore] = nanoquery({
  fetcher: () => Promise.reject('Please provide the fetcher property'),
});

const $user = createFetcherStore<{name: string}>('user', {
  fetcher: () => fetch('https://jsonplaceholder.typicode.com/users/1').then(res => res.json()),
  dedupeTime: Infinity
})

const User: FunctionComponent<{count: number}> = ({count}) => {
  const [user, setUser] = useState($user.get().data)
  useEffect(() => {
    return $user.subscribe((usr) => {
      setUser(usr.data)
    })
  }, [])
  useEffect(() => {
    console.log(user);
  }, [user])
  return !user ? 'loading...' : `${user.name} ${count}`
}

export function App() {
  const [count, setCount] = useState(0)

  useEffect(() => {
    const intervalId = setInterval(() => {
      setCount(prev => prev + 1)
    }, 200)
    return () => clearInterval(intervalId);
  })

  return (
    <>
      {count % 2 === 0 ? 'NO' : <User count={1} />}
    </>
  )
}

The console keeps logging undefined before logging the correct value, even though the fetcher is not rerunning the fetcher promise every time. I believe this is because the store resets its state to {loading: false} in this line.

From my perspective, since the data is already in the cache, the fetcherStore should return the data, not return an intermediate { loading: false} and then return the data that already exists in the cache.

My question is: Is this an intentional behaviour ? If so can you please explain the reason ?

Thank you.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions