Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
hamza0867 opened this issue Dec 6, 2024 · 0 comments

Comments

@hamza0867
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant