mirror of
https://github.com/ershisan99/flashcards-docs.git
synced 2025-12-16 20:59:26 +00:00
62 lines
1.2 KiB
Plaintext
62 lines
1.2 KiB
Plaintext
# RTK Query
|
|
|
|
## Установка
|
|
|
|
```bash filename="Terminal"
|
|
pnpm i @reduxjs/toolkit react-redux
|
|
```
|
|
|
|
## Создание стора
|
|
|
|
```ts filename="src/services/store.ts"
|
|
import { configureStore } from '@reduxjs/toolkit'
|
|
|
|
export const store = configureStore({
|
|
reducer: {},
|
|
middleware: getDefaultMiddleware => getDefaultMiddleware(),
|
|
})
|
|
|
|
export type AppDispatch = typeof store.dispatch
|
|
export type RootState = ReturnType<typeof store.getState>
|
|
```
|
|
|
|
## Подключение стора к приложению
|
|
|
|
```tsx filename="src/App.tsx"
|
|
import { Provider } from 'react-redux'
|
|
|
|
import { Router } from '@/router'
|
|
import { store } from '@/services/store'
|
|
|
|
export function App() {
|
|
return (
|
|
<Provider store={store}>
|
|
<Router />
|
|
</Provider>
|
|
)
|
|
}
|
|
```
|
|
|
|
## Создание Api
|
|
|
|
```ts filename="src/services/auth/auth.ts"
|
|
import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react'
|
|
|
|
export const authApi = createApi({
|
|
reducerPath: 'authApi',
|
|
baseQuery: fetchBaseQuery({
|
|
baseUrl: 'https://api.flashcards.andrii.es',
|
|
credentials: 'include',
|
|
}),
|
|
endpoints: builder => {
|
|
return {
|
|
getMe: builder.query<any, void>({
|
|
query: () => `auth/me`,
|
|
}),
|
|
}
|
|
},
|
|
})
|
|
|
|
export const { useGetMeQuery } = authApi
|
|
```
|