A comprehensive tutorial application demonstrating RTK Query (Redux Toolkit Query) for efficient data fetching and caching in React applications. This project showcases fundamental RTK Query concepts including queries, mutations, and automatic re-fetching.
- Data Fetching: Fetch posts from JSONPlaceholder API using RTK Query
- Data Creation: Create new posts with optimistic updates
- Automatic Caching: Built-in caching and background synchronization
- Loading States: Proper loading and error state management
- TypeScript Support: Fully typed with TypeScript for better development experience
- Auto Refetch: Automatic refetching when the window regains focus
- React 19 - Frontend framework
- Redux Toolkit - State management
- RTK Query - Data fetching and caching
- TypeScript - Type safety
- React Redux - React bindings for Redux
- JSONPlaceholder - Fake REST API for testing
- Node.js (version 16 or higher)
- npm or yarn package manager
- Clone the repository:
git clone <repository-url>
cd rtk-query-tutorial- Install dependencies:
npm install- Start the development server:
npm start- Open http://localhost:3000 to view the application.
src/
├── App.tsx # Main application component
├── store.ts # Redux store configuration
├── types.ts # TypeScript type definitions
├── servicers/
│ └── jsonPlaceholderApi.ts # RTK Query API definitions
└── index.tsx # Application entry point
Defines the API slice with:
- Base URL: JSONPlaceholder API endpoint
- getPosts Query: Fetches all posts
- createPosts Mutation: Creates new posts
- Auto-refetch: Enabled on window focus
- Configures Redux store with RTK Query
- Sets up middleware for caching and background sync
- Enables automatic query lifecycle management
- Demonstrates
useGetPostsQueryhook for data fetching - Shows
useCreatePostsMutationhook for data creation - Handles loading states and error conditions
- Provides form for creating new posts
This tutorial demonstrates:
- Setting up RTK Query in a React application
- Creating API slices with queries and mutations
- Using generated hooks in React components
- Managing loading and error states automatically
- Implementing optimistic updates with mutations
- Configuring automatic refetching behavior
Runs the app in development mode on http://localhost:3000
Launches the test runner in interactive watch mode
Builds the app for production to the build folder
Note: This is a one-way operation! Ejects from Create React App setup
The application integrates with JSONPlaceholder, a fake REST API that provides:
- GET /posts - Retrieve all posts
- POST /posts - Create a new post
- Automatic Caching: Queries are cached and shared across components
- Background Updates: Data refetches automatically on focus
- Optimistic Updates: UI updates immediately on mutations
- Error Handling: Built-in error state management
- Loading States: Automatic loading indicators
- TypeScript Integration: Fully typed API responses and requests
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is open source and available under the MIT License.