π§ State Management
Shipnative employs a powerful and efficient state management strategy using a combination of Zustand for global client-side state and React Query for server-side state and data fetching. This approach provides a clear separation of concerns, optimizes performance, and simplifies data management in your application.Zustand: Global Client-Side State
Zustand is a small, fast, and scalable bear-bones state-management solution. Itβs ideal for managing UI state, user preferences, and any other client-side data that doesnβt require server interaction.Key Features
- Simple API: Easy to learn and use, with a minimal boilerplate.
- Hooks-based: Integrates seamlessly with Reactβs functional components.
- Performant: Renders components only when necessary, optimizing performance.
- Scalable: Suitable for small to large applications.
Usage Pattern
- Define Your Store: Create a store using
createfromzustand. - Use in Components: Access state and actions using the custom hook.
Best Practices with Zustand
- Atomic Stores: Create small, focused stores for specific pieces of state.
- Selectors: Use selectors to extract only the necessary parts of the state, preventing unnecessary re-renders.
- Immutability: Always update state immutably (e.g., using spread syntax).
React Query: Server-Side State & Data Fetching
React Query (also known as TanStack Query) is a powerful library for managing, caching, synchronizing, and updating server state in your React Native applications. It handles the complexities of data fetching, leaving you with more time to focus on your UI.Key Features
- Automatic Caching: Caches fetched data, reducing redundant network requests.
- Background Refetching: Automatically refetches stale data in the background.
- Optimistic Updates: Provides a smooth user experience by updating the UI before the server responds.
- Error Handling: Built-in mechanisms for handling and retrying failed requests.
- Pagination & Infinite Scrolling: Simplifies complex data fetching patterns.
Usage Pattern
- Define Your Query Function: A simple async function that fetches data.
- Use in Components with
useQueryoruseMutation:
Best Practices with React Query
- Query Keys: Use descriptive and consistent query keys for effective caching and invalidation.
- Query Invalidation: Invalidate queries after mutations to ensure your UI reflects the latest server state.
- Optimistic Updates: Implement optimistic updates for a snappier user experience, especially for actions like βlikingβ a post.
- Error Boundaries: Use React Error Boundaries to gracefully handle data fetching errors.
Combining Zustand and React Query
Zustand and React Query complement each other perfectly:- Zustand: Manages transient UI state (e.g., modal open/close, form input values before submission, theme preferences).
- React Query: Manages persistent server data (e.g., lists of items, user profiles fetched from an API, authentication status from a backend).
Resources
- Zustand Documentation: https://zustand-demo.pmnd.rs/
- React Query Documentation: https://tanstack.com/query/latest
- Shipnative Stores:
apps/app/app/stores/(for Zustand examples) - Shipnative Services:
apps/app/app/services/(for data fetching examples with React Query)