I created the endpoint with createApi:
export const postsApi = createApi({
reducerPath: 'postsApi',
baseQuery: fetchBaseQuery({baseUrl: 'https://jsonplaceholder.typicode.com/'}),
tagTypes: ['Post'],
endpoints: builder => ({
getPosts: builder.query<Post[], void>({
query: () => '/posts',
providesTags: ['Post'],
}),
}),
});
export const {useGetPostsQuery} = postsApi;
How can I use hook useGetPostsQuery()
in the component only when a button is pressed and not when component is mounted?
I tried to add this into the component and it works, but I'm not sure if it's the best practice:
const [click, setClick] = useState<boolean>(true);
const {data, error, isLoading} = useGetPostsQuery(undefined, {skip: click});