反应Naitve Redux getState未定义

时间:2019-06-24 03:00:41

标签: react-native react-redux redux-persist

我正在尝试使用React-redux进行Firebase电话身份验证。但是我在操作文件中得到了未定义的getState。

我得到“ confirmResult.confirm未定义”

 Action.Js
 const onCodeDispatched = (code) => {
    return (dispatch, getState) => {
        console.log(getState())
        getState().auth.confirmResult.confirm(code)
        .then(user => onLoginSuccess(dispatch, user))
        .catch(error => onCodeConfirmError(dispatch, error));
    }    
}

App.js
import React from 'react'
import {Provider} from 'react-redux'
import {createStore, applyMiddleware, compose} from 'redux'
import thunkMiddleware from 'redux-thunk'

import reducers from '../reducers'
import App from './App'

import {persistStore} from 'redux-persist'
import {PersistGate} from 'redux-persist/es/integration/react'

const middleware = [thunkMiddleware]
const store = compose(applyMiddleware(...middleware))(createStore)(reducers)
console.log(store)
let persistor = persistStore(store)

class Root extends React.PureComponent {
  render() {
    return (
      <Provider store={store}>
        <PersistGate persistor={persistor}>
          <App />
        </PersistGate>
      </Provider>
    )
  }
}

export default Root

我是react-redux的新手,这让我感到非常困惑,我已经读了很多文章,我已经花了很多天寻找错误了,您能告诉我为什么getState返回Undefined吗。

1 个答案:

答案 0 :(得分:0)

尝试分拆商店的建设方式。您可以开始创建一个简单的store实例,如下所示:

const store = createStore(reducers, yourInitialState)

然后打印商店,看看发生了什么。如果一切正常,请尝试像这样使用composeapplyMiddlewarethunk

const store = createStore(
  reducer,
  compose(
    applyMiddleware(thunk),
    DevTools.instrument() // this is a really helpful instrument
  )
)

如果您有疑问,请询问或遵循Redux website中的官方文档