-
Notifications
You must be signed in to change notification settings - Fork 15
Open
Description
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.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels