Sorry if this is coming out as a silly question. I was using redux toolkit query in my project like this
const [trigger, result] = useLazyGetPostsQuery();
then in my component, I am using the data
field inside the result to render my component in react.
I have a button pressing on which the trigger
method is called.
It works fine for the first time. But for other times, an API call is made but the component does not re-renders.
I have results from both calls inside the redux store but the component prefers the first one only. How can I make it so that only new results are used and old results are removed from the cache?
rest of the code for some more clarity
API
const postsApi = createApi({
baseQuery: fetchBaseQuery({
baseUrl: '/',
}),
tagTypes: ['Post'],
endpoints: (build) => ({
getPosts: build.query({
query: (id) => ({ url: `post/${id}` }),
})
})
});
export { useLazyGetPostsQuery } = postsApi;
Component
import { useLazyGetPostsQuery } from '../../service/posts'
const PostDetails = () => {
const [trigger, result] = useLazyGetPostsQuery();
const getNewPosts = userid => {
trigger(Math.round(Math.random() * 10 + 1))
}
return (
<>
<button onClick={getNewPosts}>Click me!</button>
{results.data.map(post => <div>{post.title}</div>)}
</>
)
}