Blog>
Snippets

Proper Use of Query Keys

Demonstrate setting up query keys using arrays to ensure unique identifiers for each query.
const fetchUserData = (userId) => `user-data/${userId}`;
Defines a function to create a unique query key for fetching user data by using the user ID.
const fetchProjectDetails = (projectId) => [`project-details`, projectId];
Sets up a query key for fetching project details. The key is an array with a base string and the project ID, ensuring uniqueness per project.
const userQueryKey = fetchUserData(123);
Generates a unique query key for a user with ID 123 by invoking the fetchUserData function.
const projectQueryKey = fetchProjectDetails('projectA');
Creates a unique query key for the project with ID 'projectA' by calling fetchProjectDetails, demonstrating the array-based approach.