到目前为止,这是我的代码:
import { createStore, applyMiddleware, compose } from 'redux'
import Rehydration from '../Services/Rehydration'
import ReduxPersist from '../Config/ReduxPersist'
import Config from '../Config/DebugConfig'
import createSagaMiddleware from 'redux-saga'
import ScreenTracking from './ScreenTrackingMiddleware'
import { createReactNavigationReduxMiddleware } from 'react-navigation-redux-helpers'
// creates the store
export default (rootReducer, rootSaga) => {
/* ------------- Redux Configuration ------------- */
const middleware = []
const enhancers = []
/* ------------- Navigation Middleware ------------ */
const navigationMiddleware = createReactNavigationReduxMiddleware(
'root',
state => state.nav
)
middleware.push(navigationMiddleware)
/* ------------- Analytics Middleware ------------- */
middleware.push(ScreenTracking)
/* ------------- Saga Middleware ------------- */
const sagaMonitor = Config.useReactotron ? console.tron.createSagaMonitor() : null
const sagaMiddleware = createSagaMiddleware({ sagaMonitor })
middleware.push(sagaMiddleware)
/* ------------- Assemble Middleware ------------- */
enhancers.push(applyMiddleware(...middleware))
// if Reactotron is enabled (default for __DEV__), we'll create the store through Reactotron
const createAppropriateStore = Config.useReactotron ? console.tron.createStore : createStore
const store = createAppropriateStore(rootReducer, compose(...enhancers))
// configure persistStore and check reducer version number
if (ReduxPersist.active) {
Rehydration.updateReducers(store)
}
// kick off root saga
let sagasManager = sagaMiddleware.run(rootSaga)
return {
store,
sagasManager,
sagaMiddleware
}
}
但是出现以下错误:
错误:在调用“ reduxfiyNavigator”之前,
请致电“ createReactNavigationReduxMiddleware”,以便我们知道何时触发您的监听器。
我添加了详细的代码here at GitHUB。
我正在使用称为androdss的 ignite react native入门工具包。
感谢您的帮助。
答案 0 :(得分:5)
根据错误,您需要在createReactNavigationReduxMiddleware
之前初始化reduxfiyNavigator
。
可能还有其他替代方法,但是这是我解决的方法。
根据ignite-cli
androdss
生成器。
定义您的middleware
并将navigator
包装到reduxify
。
import { createStackNavigator } from 'react-navigation'
import LaunchScreen from '../Containers/LaunchScreen'
import { createReactNavigationReduxMiddleware, reduxifyNavigator } from 'react-navigation-redux-helpers'
import styles from './Styles/NavigationStyles'
// Manifest of possible screens
export const PrimaryNav = createStackNavigator({
LaunchScreen: { screen: LaunchScreen }
}, {
// Default config for all screens
headerMode: 'none',
initialRouteName: 'LaunchScreen',
navigationOptions: {
headerStyle: styles.header
}
})
// Create middleware and connect
export const appNavigatorMiddleware = createReactNavigationReduxMiddleware(
'root',
state => state.nav
);
const AppNavigator = reduxifyNavigator(PrimaryNav, 'root');
export default AppNavigator
按照react-navigation
import {PrimaryNav} from '../Navigation/AppNavigation'
export const reducer = (state, action) => {
const newState = PrimaryNav.router.getStateForAction(action, state)
return newState || state
}
自从addNavigationHelpers
react-navigation
作为名称为
import React from 'react'
import { BackHandler, Platform } from 'react-native'
import { connect } from 'react-redux'
import AppNavigation from './AppNavigation'
class ReduxNavigation extends React.Component {
componentWillMount () {
if (Platform.OS === 'ios') return
BackHandler.addEventListener('hardwareBackPress', () => {
const { dispatch, nav } = this.props
// change to whatever is your first screen, otherwise unpredictable results may occur
if (nav.routes.length === 1 && (nav.routes[0].routeName === 'LaunchScreen')) {
return false
}
// if (shouldCloseApp(nav)) return false
dispatch({ type: 'Navigation/BACK' })
return true
})
}
componentWillUnmount () {
if (Platform.OS === 'ios') return
BackHandler.removeEventListener('hardwareBackPress')
}
render () {
return <AppNavigation state={this.props.nav} dispatch={this.props.dispatch}/>
}
}
const mapStateToProps = state => ({ nav: state.nav })
export default connect(mapStateToProps)(ReduxNavigation)
添加导出中间件以进行初始化
import { createStore, applyMiddleware, compose } from 'redux'
import Rehydration from '../Services/Rehydration'
import ReduxPersist from '../Config/ReduxPersist'
import Config from '../Config/DebugConfig'
import createSagaMiddleware from 'redux-saga'
import ScreenTracking from './ScreenTrackingMiddleware'
import {appNavigatorMiddleware} from '../Navigation/AppNavigation'
// creates the store
export default (rootReducer, rootSaga) => {
/* ------------- Redux Configuration ------------- */
const middleware = []
const enhancers = []
/* ------------- Navigation Middleware ------------ */
middleware.push(appNavigatorMiddleware)
/* ------------- Analytics Middleware ------------- */
middleware.push(ScreenTracking)
/* ------------- Saga Middleware ------------- */
const sagaMonitor = Config.useReactotron ? console.tron.createSagaMonitor() : null
const sagaMiddleware = createSagaMiddleware({ sagaMonitor })
middleware.push(sagaMiddleware)
/* ------------- Assemble Middleware ------------- */
enhancers.push(applyMiddleware(...middleware))
// if Reactotron is enabled (default for __DEV__), we'll create the store through Reactotron
const createAppropriateStore = Config.useReactotron ? console.tron.createStore : createStore
const store = createAppropriateStore(rootReducer, compose(...enhancers))
// configure persistStore and check reducer version number
if (ReduxPersist.active) {
Rehydration.updateReducers(store)
}
// kick off root saga
let sagasManager = sagaMiddleware.run(rootSaga)
return {
store,
sagasManager,
sagaMiddleware
}
}
答案 1 :(得分:-1)
我建议您检查两件事:
1)reduxifyNavigator
的第二个参数与createReactNavigationReduxMiddleware
的第一个参数,即root
相同。另外,我建议将所有常量移动到单独的文件中,例如'constants.js',并导入其定义以在您的代码中使用。它消除了此类错误的可能性。
2)从根源文件开始跟踪应用程序中的导入顺序,并确保在导航之前导入商店,否则reduxifyNavigator
将在createReactNavigationReduxMiddleware
之前被调用,这将触发这个错误。