Next.js,Redux,Redux-saga,@ reduxjs / toolkit生成存储-没有重载与此调用匹配

时间:2020-03-30 21:43:15

标签: javascript reactjs react-redux next.js redux-saga

嗨,我正在配置Redux存储,我需要做出类似的想法

import { configureStore, getDefaultMiddleware } from '@reduxjs/toolkit'
import createSagaMiddleware from 'redux-saga'
import logger from 'redux-logger'
import { reduxBatch }  from '@manaflair/redux-batch';

import rootSaga from '../sagas/root-saga'

import rootReducer from './rootReducer'

const makeStore = (initialState, options) => {
    const sagaMiddleware = createSagaMiddleware()

    const middleware = [...getDefaultMiddleware(), logger, sagaMiddleware]

    const preloadedState = {}

    const store = configureStore({
        reducer: rootReducer,
        middleware,
        devTools: process.env.NODE_ENV !== 'production',
        preloadedState,
        enhancers: [reduxBatch]
    })

    sagaMiddleware.run(rootSaga)

    return store
}

// export type AppDispatch = typeof makeStore.dispatch

export type AppStore = typeof makeStore

export default makeStore

我想使用redux-saga,@ reduxjs / toolkit,Next.js,redux

_app.tsx

import React from 'react'
import { Container, AppProps } from 'next/app'
import { Provider } from 'react-redux'
import withRedux from 'next-redux-wrapper'
import withReduxSaga from 'next-redux-saga'
import { ConnectedRouter } from 'connected-next-router'

import GlobalStyles from '../src/globalStyles'
// import { ErrorBoundary } from '../src/components/ErrorBoundary'
import makeStore, { AppStore } from '../src/store'

interface Props extends AppProps {
    store: AppStore
}

const MyApp = ({ Component, pageProps, store }: Props) => {
    return (
        <Container>
            <Provider store={store}>
                <ConnectedRouter>
                    <GlobalStyles/>
                    {/*<ErrorBoundary>*/}
                    {/*<Layout>*/}
                    <Component {...pageProps} />
                    {/*</Layout>*/}
                    {/*</ErrorBoundary>*/}
                </ConnectedRouter>
            </Provider>
        </Container>
    )
}

export default withRedux(makeStore)(withReduxSaga(MyApp))

但是我在商店中遇到错误

No overload matches this call.
  Overload 1 of 2, '(props: Readonly<ProviderProps<AnyAction>>): Provider<AnyAction>', gave the following error.
    Type '(initialState: any, options: any) => EnhancedStore<{ readonly [$CombinedState]?: undefined; }, AnyAction, (Middleware<{}, any, Dispatch<AnyAction>> | Middleware)[]>' is missing the following properties from type 'Store<any, AnyAction>': dispatch, getState, subscribe, replaceReducer, [Symbol.observable]
  Overload 2 of 2, '(props: ProviderProps<AnyAction>, context?: any): Provider<AnyAction>', gave the following error.
    Type '(initialState: any, options: any) => EnhancedStore<{ readonly [$CombinedState]?: undefined; }, AnyAction, (Middleware<{}, any, Dispatch<AnyAction>> | Middleware)[]>' is not assignable to type 'Store<any, AnyAction>'.

我尝试了here中的示例,但是在Redux中不使用Next.js,因此我需要将这两种方式结合起来。

我不知道它应该是什么样子。有人能帮助我吗? :)

更新!

我解决了配置store.ts

的问题
import { configureStore, getDefaultMiddleware } from '@reduxjs/toolkit'
import createSagaMiddleware from 'redux-saga'
import logger from 'redux-logger'
import { reduxBatch }  from '@manaflair/redux-batch';

import rootSaga from '../sagas/root-saga'

import rootReducer from './rootReducer'

const sagaMiddleware = createSagaMiddleware()

const middleware = [...getDefaultMiddleware(), logger, sagaMiddleware]

const preloadedState = {}

const store = configureStore({
    reducer: rootReducer,
    middleware,
    devTools: process.env.NODE_ENV !== 'production',
    preloadedState,
    enhancers: [reduxBatch]
})

// @ts-ignore TODO: resolve 
store.sagaTask = sagaMiddleware.run(rootSaga)

export type AppDispatch = typeof store.dispatch

export type AppStore = typeof store

export default store

并更改了_app.tsx

const makeStore = () => store

export default withRedux(makeStore)(withReduxSaga(MyApp))

0 个答案:

没有答案