首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >最适合存储在Next.js项目+ TypeScript中的类型

最适合存储在Next.js项目+ TypeScript中的类型
EN

Stack Overflow用户
提问于 2021-10-09 07:07:04
回答 2查看 35关注 0票数 1

export let store: any;store的合适类型是什么,而不是any?假设我在最后一行使用了@ts-igore。我可以做些什么来消除这个TypeScript错误呢?我在前三行提到了我在这个项目中使用的样板。这是Next.js项目+ TypeScript + redux-presist的一部分。

代码语言:javascript
复制
// ./stores/store.ts

// Boilerplate for Redux + Next.js: https://github.com/vercel/next.js/tree/canary/examples/with-redux-wrapper
// Boilerplate for Redux Persist: https://github.com/fazlulkarimweb/with-next-redux-wrapper-redux-persist/blob/master/store/store.js
// For more information check next-redux-wrapper: https://github.com/kirill-konshin/next-redux-wrapper

import {
  createStore,
  applyMiddleware,
  combineReducers,
  Middleware,
} from 'redux';
import { createWrapper } from 'next-redux-wrapper';
import thunkMiddleware from 'redux-thunk';

import auth from './auth/reducer';
import userData from './user/reducer';
import userChallenges from './userChallenges/reducer';
import challenges from './challenges/reducer';

// Binding middleware
const bindMiddleware = (middleware: Middleware[]) => {
  if (process.env.NODE_ENV !== 'production') {
    const { composeWithDevTools } = require('redux-devtools-extension');
    return composeWithDevTools(applyMiddleware(...middleware));
  }
  return applyMiddleware(...middleware);
};

// Combining all reducers
const combinedReducer = combineReducers({
  auth,
  userData,
  userChallenges,
  challenges,
});

export let store: any;

const makeStore = ({ isServer }: { isServer: boolean }) => {
  if (isServer) {
    //If it's on server side, create a store
    return createStore(combinedReducer, bindMiddleware([thunkMiddleware]));
  } else {
    //If it's on client side, create a store which will persist
    const { persistStore, persistReducer } = require('redux-persist');
    const storage = require('redux-persist/lib/storage').default;

    const persistConfig = {
      key: 'nextjs',
      whitelist: ['auth', 'userData'], // only these reducers will be persisted, add other reducers if needed
      storage, // if needed, use a safer storage
    };

    const persistedReducer = persistReducer(persistConfig, combinedReducer); // Create a new reducer with our existing reducer

    store = createStore(persistedReducer, bindMiddleware([thunkMiddleware])); // Creating the store again

    store.__persistor = persistStore(store); // This creates a persistor object & push that persisted object to .__persistor, so that we can avail the persistability feature

    return store;
  }
};

// Export the wrapper & wrap the pages/_app.js with this wrapper only
// @ts-ignore
export const wrapper = createWrapper(makeStore);
EN

回答 2

Stack Overflow用户

发布于 2021-10-09 22:30:59

我对redux的理解是,存储就是数据对象。所以它完全由你来定义。

代码语言:javascript
复制
interface StoreType {
  label: string;
}

假设上面是您的数据结构

票数 1
EN

Stack Overflow用户

发布于 2021-10-14 08:33:03

代码语言:javascript
复制
 export const makeStore = (context: Context) => {
     const sagaMiddleware = createSagaMiddleware();
      const store = createStore(
        rootReducer,
        // list of your middlewares here
        bindMiddleware([thunk, sagaMiddleware])
      );
      // in case someone else uses saga
      (store as SagaStore).sagaTask = sagaMiddleware.run(rootSaga);

  return store;
};

您的rootReducer:

代码语言:javascript
复制
const rootReducer = combineReducers({
  productList: productListReducer,
  cart: cartReducer,
  user: userLoginReducer,
  userDetail: userDetailReducer,
});

和你的RootState

代码语言:javascript
复制
export type RootState = ReturnType<typeof rootReducer>;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69504502

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档