3

I'm using react-query in a Posts component that renders a PostForm component followed by a list of PostView components.

--Posts--

const [ isLoading, data ] = useQuery("get_posts", {...}, )

const [ userPosts, setUserPosts] = useState([]);
const [ isReady, setIsReady ] = useState(false);

useeffect(() => {
    newPosts = ..do some things with the data
    setUserPosts(newPosts)
    setIsReady(true);
}, [isLoading])

return !isReady ? null : (
    <PostForm />
    posts.current.map(post => <PostView key={post.id} Content={post} />
)

--PostForm--

const mutation = useMutation(
    () => { ... },
    { onSuccess: queryClient.invalidateQueries("get_posts")}
)

const submitHandler = e => {
    const newPost = ...
    mutation.mutate(newPost)
}

return ( a form wired to the submitHandler )

When the mutation is called in PostForm and the query in Posts gets invalidated I can see the new GET request being made with the new post added in, but I also would expect isLoading being set to true and then false and that to trigger the dependency array of the useEffect hook in Posts.

Isn't the isLoading value being reset while the query is refetched? How can I trigger a re-render on my Posts component when the mutation invalidates the previous query results?

1 Answer 1

4

isLoading is only set to true if there is no data before while fetching, like when you load your app at the first time. If there is already data (including cache data), and you're doing subsequent refetches, you can use isFetching to indicate the fetching state.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.