【问题标题】:Unhandled JS Exception: Could not find "store" in either the context or props未处理的 JS 异常:在上下文或道具中找不到“商店”
【发布时间】:2017-04-20 09:25:24
【问题描述】:

它在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
}

编辑

Redux/index.js

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

  return configureStore(rootReducer, rootSaga)
}

【问题讨论】:

  • 你能展示一下 Redux.js 文件里面的内容吗?
  • @HenrikR 我已经添加了代码

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


【解决方案1】:

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

看起来您正在使用样板启动器,其中包含许多您还不理解的代码,这很好。在学习 Redux 时,我建议查看他们自己的文档并真正了解发生了什么:)

【讨论】:

  • 我确实在使用样板文件(Ignite)。他们在 Redux 文件夹中有一个 index.js 文件,该文件使用减速器创建存储(参见编辑)。奇怪的是,它在 Android 中也能运行
  • 我发现了问题,这对我来说真的很愚蠢。我将ios 文件链接到错误的文件(不是带有Provider 的文件)。它现在正在工作。感谢您的帮助并链接到官方文档
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-09-28
  • 1970-01-01
  • 2018-04-30
  • 2018-08-02
  • 2017-06-13
  • 2016-07-12
  • 2023-03-24
相关资源
最近更新 更多