rgommezz / react-native-offline

Handy toolbelt to deal nicely with offline/online connectivity in a React Native app. Smooth redux integration ✈️
MIT License
2.34k stars 272 forks source link

[Question] Compatibility with RTK Query #353

Closed cseelus closed 2 years ago

cseelus commented 2 years ago

Did some testing and the approach react-native-offline takes to ease the creation of offline first apps looks very interesting, especially the Redux integration. I could not find any information regarding RTK Query anywhere so far though.

RTK Query is a relatively new 'data fetching and caching tool' from the Redux team, 'eliminating the need to hand-write data fetching & caching logic yourself'.

By very briefly defining queries and mutations to specific API endpoints like /comments, RTK Query augments an applications Redux store with matching reducers/actions and provides autogenerated hooks for each endpoint.

Basic example:


// RTK Query API
import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query'

const api = createApi({
  baseQuery: fetchBaseQuery({
    baseUrl: 'https://some.url',
  }),
  endpoints: (builder) => ({
    getComments: builder.query({
      query: () => ({ url: '/comments' })
    }),
    saveComment: builder.mutation({
      query: (comment) => ({
        url: '/comments',
        method: 'POST',
        body: comment,
      })
    })
  })
})

export const {
  useGetCommentsQuery,
  useSaveCommentMutation
} = api

// Redux Toolkit store
import { configureStore } from '@reduxjs/toolkit'
import { api } from './api'

export const store = configureStore({
  reducer: {
    [api.reducerPath]: api.reducer,
  },
  middleware: (getDefaultMiddleware) =>
    getDefaultMiddleware().concat(
      api.middleware
    )
})

@rgommezz: Now the question of course is, do you have any knowledge/experience if react-native-offline is compatible with RTK Query (yet), particularly if there is a proper way to add to the actionQueue as part of the network state, when an app is offline?

stale[bot] commented 2 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] commented 2 years ago

Closing this issue after a prolonged period of inactivity. If this is still present in the latest release, please feel free to create a new issue with up-to-date information.

blwinters commented 1 year ago

I think this is an important question. I hope we can at least get some response on whether supporting RTK Query is of interest to the maintainers.

ElForastero commented 1 year ago

RTKQ doesn't have offline mode support.

It means it cannot detect network status updates and pause/resume actions queue or even outgoing network requests.

There's also no way to change this behavior with configuration. The only thing I saw is that hook that is used to build request headers may be used to pause request until some promise resolves. But it sounds more like a hack.

Moreover, the RTKQ API was designed to support only predefined set of thunk actions, like /pending, /fulfilled and /rejected. I think there's no way to use RTKQ with any kind of offline middleware at the moment.