1

用 redux 配置 firebase 对我来说很痛苦。

我不知道怎么了,但我觉得很卡。我正在尝试传递getFirebasegetFirestore作为参数,thunk但我遇到了这个错误:

在此处输入图像描述

在尝试将额外的参数添加到 thunk 之前,我让它工作了。我正在使用下面的代码:

import { createStore, applyMiddleware, compose } from 'redux'
import thunk from 'redux-thunk'
import { composeWithDevTools } from 'redux-devtools-extension'
import rootReducer from './modules'
import {reduxFirestore, getFirestore} from 'redux-firestore'
import {reactReduxFirebase, getFirebase} from 'react-redux-firebase'
import fbConfig from './firebase/config'

export function initializeStore(initialState) {
  const _thunk = thunk.withExtraArgument({ getFirebase, getFirestore })

  const middlewareEnhancer = applyMiddleware(_thunk)

  const composedEnhancers = compose(
    middlewareEnhancer,
    reduxFirestore(fbConfig),
    reactReduxFirebase(fbConfig)
  )

  const store = createStore(rootReducer, initialState, composedEnhancers)

  return store
}

你知道会发生什么吗?我很绝望。

太感谢了。

4

1 回答 1

0

如果你提到'redux-firestore'和'react-redux-firebase'的版本会很棒。

最近有一个 react-redux v6 迁移: http ://react-redux-firebase.com/docs/v3-migration-guide.html

您可能需要在代码中进行的更改:

- import { reactReduxFirebase, getFirebase } from 'react-redux-firebase'
+ import { ReactReduxFirebaseProvider, getFirebase } from 'react-redux-firebase'

- import { reduxFirestore, getFirestore } from 'redux-firestore'
+ import { createFirestoreInstance, reduxFirestore, getFirestore } from 'redux-firestore'

+ import firebase from "firebase/app";

当你有 react-redux-firebase 的最新版本时使用它,因为 reactReduxFirebase 的 API 已删除。还要改变composedEnhancers如下:

- const composedEnhancers = compose(
-    middlewareEnhancer,
-    reduxFirestore(fbConfig),
-    reactReduxFirebase(fbConfig)
-   )

+  const composedEnhancers = compose(
+     middlewareEnhancer,
+     reduxFirestore(firebase, fbConfig)
+  )

添加 react-redux-firebase 道具

+ const rrfProps = {
+   firebase,
+   config: fbConfig,
+   dispatch: store.dispatch, 
+   createFirestoreInstance,
+ }

您需要为您的应用提供 react-redux-firebase 道具:

<ReactReduxFirebaseProvider {...rrfProps}>
        <App />
</ReactReduxFirebaseProvider>

它应该可以正常工作。您可以按照上面给出的链接中的说明添加 rrfConfig,以防它仍然不起作用。

于 2020-05-22T17:11:35.277 回答