未处理的JS异常:无法在上下文或道具中找到“store”

时间:2016-12-05 21:17:47

标签: android ios react-native redux react-redux

它在Android上运行良好,但在iOS上运行在启动时失败。它给我留下了一个空白的屏幕。

  

未处理的JS异常:无法在“Connect(t)”的上下文或道具中找到“store”。将根组件包装在a中,或者将“store”显式传递为“Connect(t)”的支柱。

我将store作为道具:

import React, { Component } from 'react'
import { Provider } from 'react-redux'
import RootContainer from './Root'
import createStore from './Redux'
// import applyConfigSettings from './config'
// applyConfigSettings()

const store = createStore()

class App extends Component {
  render () {
    return (
      <Provider store={store}>
        <RootContainer />
      </Provider>
    )
  }
}

export default App

RootContainer

class Root extends Component {

  componentDidMount () {
    if (!ReduxPersist.active) {
      this.props.startup()
    }
  }

  render() {
    const scenes = Pages;
    return (
      <View style={{flex:1}}>
        <StatusBar hidden={ true } />
        <Router scenes={scenes} navigationBarStyle={ Style.header } titleStyle={ Style.header_title } />
        <Toast ref="toast" style={ Style.toast } position='bottom'/>
      </View>
    );
  }

}


const mapStateToDispatch = dispatch => ({
  startup: () => dispatch(StartupActions.startup())
})

export default connect(null, mapStateToDispatch)(Root)

Redux.js

import { createStore, applyMiddleware, compose } from 'redux'
import { autoRehydrate } from 'redux-persist'
import createLogger from 'redux-logger'
import Config from './../config/DebugSettings'
import createSagaMiddleware from 'redux-saga'
import R from 'ramda'
import RehydrationServices from './../services/RehydrationServices'
import ReduxPersist from './../config/ReduxPersist'
import { StartupTypes } from './StartupRedux'

// creates the store
export default (rootReducer, rootSaga) => {
  /* ------------- Redux Configuration ------------- */

  const middleware = []
  const enhancers = []

  /* ------------- Saga Middleware ------------- */

  const sagaMiddleware = createSagaMiddleware()
  middleware.push(sagaMiddleware)

  /* ------------- Logger Middleware ------------- */

  const SAGA_LOGGING_BLACKLIST = ['EFFECT_TRIGGERED', 'EFFECT_RESOLVED', 'EFFECT_REJECTED', 'persist/REHYDRATE']
  if (__DEV__) {
    // the logger master switch
    const USE_LOGGING = Config.reduxLogging
    // silence these saga-based messages
    // create the logger
    const logger = createLogger({
      predicate: (getState, { type }) => USE_LOGGING && R.not(R.contains(type, SAGA_LOGGING_BLACKLIST))
    })
    middleware.push(logger)
  }

  /* ------------- Reactotron Enhancer ------------- */

  // in dev, let's bring **START** with Reactotron's store enhancer
  if (__DEV__) {
    // only bring in Reactotron in dev mode
    // const createReactotronEnhancer = require('reactotron-redux')
    //
    // // create it
    // const reactotronEnhancer = createReactotronEnhancer(console.tron, {
    //   // you can flag some of your actions as important by returning true here
    //   isActionImportant: action =>
    //     action.type === StartupTypes.STARTUP,
    //
    //   // you can flag to exclude certain types too... especially the chatty ones
    //   except: [...SAGA_LOGGING_BLACKLIST]
    // })
    // enhancers.push(reactotronEnhancer)
  }

  /* ------------- Assemble Middleware ------------- */

  enhancers.push(applyMiddleware(...middleware))

  /* ------------- AutoRehydrate Enhancer ------------- */

  // add the autoRehydrate enhancer
  if (ReduxPersist.active) {
    enhancers.push(autoRehydrate())
  }

  const store = createStore(rootReducer, compose(...enhancers))

  // configure persistStore and check reducer version number
  if (ReduxPersist.active) {
    RehydrationServices.updateReducers(store)
  }

  // kick off root saga
  sagaMiddleware.run(rootSaga)

  return store
}

修改

终极版/ index.js

export default () => {
  /* ------------- Assemble The Reducers ------------- */
  const rootReducer = combineReducers({
    data: require('./DataRedux').reducer,
  })

  return configureStore(rootReducer, rootSaga)
}

1 个答案:

答案 0 :(得分:3)

您必须将rootReducer提供给从Redux.js文件导入的createStore函数。 Redux商店必须有root reducer :)你也可以从他们自己的文档中看到这个http://redux.js.org/docs/basics/Store.html

看起来你也在使用样板启动器,它有很多你还不了解的代码,这很好。学习Redux时,我建议您查看自己的文档并真正了解正在发生的事情:)