Skip to content

Instantly share code, notes, and snippets.

@AlaBenAicha
Created October 2, 2022 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AlaBenAicha/1ecf44e72a246c22ef6e29515b8b1276 to your computer and use it in GitHub Desktop.
Save AlaBenAicha/1ecf44e72a246c22ef6e29515b8b1276 to your computer and use it in GitHub Desktop.
import { configureStore } from '@reduxjs/toolkit';
import { pokemonApi } from './services/pokemon';
import { useMemo } from 'react'
let store
const initialState = {}
function initStore(preloadedState = initialState) {
return configureStore({
reducer: {
// Add the generated reducer as a specific top-level slice
[pokemonApi.reducerPath]: pokemonApi.reducer,
},
preloadedState,
// Adding the api middleware enables caching, invalidation, polling,
// and other useful features of `rtk-query`.
middleware: (getDefaultMiddleware) => getDefaultMiddleware().concat(pokemonApi.middleware),
});
}
export const initializeStore = (preloadedState) => {
let _store = store ?? initStore(preloadedState)
// After navigating to a page with an initial Redux state, merge that state
// with the current state in the store, and create a new store
if (preloadedState && store) {
_store = initStore({
...store.getState(),
...preloadedState,
})
// Reset the current store
store = undefined
}
// For SSG and SSR always create a new store
if (typeof window === 'undefined') return _store
// Create the store once in the client
if (!store) store = _store
return _store
}
export function useStore(initialState) {
const store = useMemo(() => initializeStore(initialState), [initialState])
return store
}
export function removeUndefined(state) {
if (typeof state === 'undefined') return null
if (Array.isArray(state)) return state.map(removeUndefined)
if (typeof state === 'object' && state !== null) {
return Object.entries(state).reduce((acc, [key, value]) => {
return {
...acc,
[key]: removeUndefined(value)
}
}, {})
}
return state
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment