Socket
Socket
Sign inDemoInstall

@tanstack/vue-query

Package Overview
Dependencies
Maintainers
2
Versions
296
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@tanstack/vue-query

Hooks for managing, caching and syncing asynchronous and remote data in Vue


Version published
Maintainers
2
Created

What is @tanstack/vue-query?

@tanstack/vue-query is a powerful data-fetching and state management library for Vue.js applications. It provides tools to fetch, cache, synchronize, and update server state in your application, making it easier to manage asynchronous data and improve the user experience.

What are @tanstack/vue-query's main functionalities?

Data Fetching

This feature allows you to fetch data from an API and manage the loading, error, and success states. The `useQuery` hook is used to fetch data and automatically cache it.

```javascript
import { useQuery } from '@tanstack/vue-query';

const fetchTodos = async () => {
  const response = await fetch('/api/todos');
  return response.json();
};

export default {
  setup() {
    const { data, error, isLoading } = useQuery(['todos'], fetchTodos);

    return { data, error, isLoading };
  }
};
```

Data Mutation

This feature allows you to perform mutations (create, update, delete) on your data. The `useMutation` hook is used to handle the mutation and automatically update the cache.

```javascript
import { useMutation, useQueryClient } from '@tanstack/vue-query';

const addTodo = async (newTodo) => {
  const response = await fetch('/api/todos', {
    method: 'POST',
    body: JSON.stringify(newTodo),
  });
  return response.json();
};

export default {
  setup() {
    const queryClient = useQueryClient();
    const mutation = useMutation(addTodo, {
      onSuccess: () => {
        queryClient.invalidateQueries(['todos']);
      },
    });

    return { mutation };
  }
};
```

Query Invalidation

This feature allows you to manually invalidate queries, which forces them to refetch. This is useful when you know that the data has changed and you want to ensure that your UI is up-to-date.

```javascript
import { useQueryClient } from '@tanstack/vue-query';

export default {
  setup() {
    const queryClient = useQueryClient();

    const invalidateTodos = () => {
      queryClient.invalidateQueries(['todos']);
    };

    return { invalidateTodos };
  }
};
```

Query Caching

This feature allows you to cache query results for a specified amount of time. The `staleTime` option is used to determine how long the data is considered fresh before it needs to be refetched.

```javascript
import { useQuery } from '@tanstack/vue-query';

const fetchUser = async (userId) => {
  const response = await fetch(`/api/user/${userId}`);
  return response.json();
};

export default {
  setup() {
    const { data, error, isLoading } = useQuery(['user', userId], () => fetchUser(userId), {
      staleTime: 1000 * 60 * 5, // 5 minutes
    });

    return { data, error, isLoading };
  }
};
```

Other packages similar to @tanstack/vue-query

FAQs

Package last updated on 12 Sep 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc