-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstore.ts
37 lines (30 loc) · 889 Bytes
/
store.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import { useMemo } from "react";
import { createStore, applyMiddleware } from "redux";
import { composeWithDevTools } from "redux-devtools-extension";
import thunkMiddleware from "redux-thunk";
import reducers from "./reducers";
let store;
function initStore(initialState) {
return createStore(
reducers,
initialState,
composeWithDevTools(applyMiddleware(thunkMiddleware))
);
}
export const initializeStore = (preloadedState) => {
let _store = store ?? initStore(preloadedState);
if (preloadedState && store) {
_store = initStore({
...store.getState(),
...preloadedState,
});
store = undefined;
}
if (typeof window === "undefined") return _store;
if (!store) store = _store;
return _store;
};
export function useStore(initialState) {
const store = useMemo(() => initializeStore(initialState), [initialState]);
return store;
}