React Native:nextState.routes.forEach undefined 不是一个对象
React Native: nextState.routes.forEach undefined is not an object
我正在用 React Native 构建一个移动应用程序,我遇到的一件事是导航。现在我正在尝试实现一个带有主屏幕的登录屏幕,我可以从中注销或调用其他屏幕。为此,由于我使用 redux,我尝试调整我的 StackNavigator 并为它制作一个 reducer 和一个单独的组件。但是在完成此操作后,我遇到了这个错误:
undefined is not an object (evaluating nextState.routes.forEach)
此错误是在我的 App.js 连接函数代码中触发的,该函数必须将一些状态变量映射到导航组件。这是我的代码:
NavReducer.js
import { NavigationActions, StackNavigator } from "react-navigation";
import routes from "../core/routes";
export const AppNavigator = StackNavigator(routes)
const routerForLogin = AppNavigator.router.getActionForPathAndParams('Login')
const routerForPlane = AppNavigator.router.getActionForPathAndParams('PlaneList')
const loginState = AppNavigator.router.getStateForAction(routerForLogin);
const registerState = AppNavigator.router.getStateForAction(AppNavigator.router.getActionForPathAndParams('Register'));
const planeListState = AppNavigator.router.getStateForAction(routerForPlane);
const insertState = AppNavigator.router.getStateForAction(AppNavigator.router.getActionForPathAndParams('Insert'));
export const navReducer = (state = { loginState, planeListState, insertState, registerState }, action) => {
switch (action.type) {
case '@@redux/INIT':
return {
...state,
planeListState: AppNavigator.router.getStateForAction(routerForPlane, loginState)
};
case 'LOGIN':
return {
...state,
planeListState: AppNavigator.router.getStateForAction(routerForPlane, loginState)
};
case 'LOGOUT':
return {
...state,
loginState: AppNavigator.router.getStateForAction(
NavigationActions.reset({
index: 0,
actions: [NavigationActions.navigate({ routeName: 'Login' })]
})
)
};
case 'REGISTER':
return {
...state,
registerState: AppNavigator.router.getStateForAction(action, state.registerState)
};
case 'INSERT':
return {
...state,
insertState: AppNavigator.router.getStateForAction(action, state.insertState)
};
default:
return {
...state,
planeListState: AppNavigator.router.getStateForAction(action, state.planeListState)
};
}
};
App.js
import React, { Component } from 'react';
import { createStore, applyMiddleware, combineReducers, compose } from 'redux';
import thunk from 'redux-thunk';
import { Provider, connect } from 'react-redux';
import { addNavigationHelpers } from 'react-navigation';
import authReducer from './src/reducers/LoginReducer';
import registerReducer from './src/reducers/RegisterReducer';
import planeReducer from './src/reducers/PlaneListReducer';
import insertReducer from './src/reducers/InsertReducer';
import Expo from 'expo';
import { AsyncStorage } from 'react-native'
import { navReducer,AppNavigator } from './src/reducers/NavReducer';
class App extends Component {
render() {
const { nav, dispatch } = this.props;
const state = AsyncStorage.getItem('token')
? this.props.nav.planeListState
: this.props.nav.loginState;
return (
<AppNavigator
screenProps={{ store: { store } }}
navigation={addNavigationHelpers({
dispatch: this.props.dispatch,
state: this.props.nav
})}
/>
);
}
}
const mapStateToProps = (state) => ({
nav: state.nav
});
const AppWithNavigationState = connect(mapStateToProps)(App);
const initialState = {
auth: { isLoading: false, error: null, username: '', password: '' },
insert: { wasInserted: false }
};
const rootReducer = combineReducers
(
{
nav: navReducer, auth: authReducer, register: registerReducer, planeList: planeReducer, insert: insertReducer
}
);
let store = createStore(rootReducer, initialState, applyMiddleware(thunk));
export default function Root() {
return (
<Provider store={store}>
<AppWithNavigationState />
</Provider>
);
}
Routes.js
import LoginComponent from "../components/Login";
import RegisterComponent from "../components/Register";
import PlaneListComponent from "../components/PlaneList";
import InsertComponent from "../components/Insert";
const Routes = {
Login: { screen: LoginComponent },
Register: { screen: RegisterComponent },
PlaneList: { screen: PlaneListComponent },
Insert: { screen: InsertComponent }
};
export default Routes;
我的代码有什么问题会触发该错误?谢谢。
根据您的代码,我认为问题出在您的 navReducer
我建议你将你的 navReducer 名称更改为另一个名称并组合它并创建新的 Reducer 名称 navReducer 像这样。
const navReducer = (state, action) => {
const newState = AppNavigator.router.getStateForAction(action, state);
// Simply return the original `state` if `nextState` is null or undefined.
return newState || state;
};
我还在 Github 上将我的 react-native-redux-example 推送到我的存储库
https://github.com/chen2584/react-native-redux-example
希望对您有所帮助!
刚刚经历了相同的错误消息:
在App
的render
方法中,你定义
const state = AsyncStorage.getItem('token')
? this.props.nav.planeListState
: this.props.nav.loginState;
但是 AppNavigator
中的 addNavigationHelpers
属性正在传递参数 state: this.props.nav
。
我的猜测是您打算传递之前定义的 state
(您当前未使用)。试试这个:
<AppNavigator
screenProps={{ store: { store } }}
navigation={addNavigationHelpers({
dispatch: this.props.dispatch,
state
})}
/>
我正在用 React Native 构建一个移动应用程序,我遇到的一件事是导航。现在我正在尝试实现一个带有主屏幕的登录屏幕,我可以从中注销或调用其他屏幕。为此,由于我使用 redux,我尝试调整我的 StackNavigator 并为它制作一个 reducer 和一个单独的组件。但是在完成此操作后,我遇到了这个错误:
undefined is not an object (evaluating nextState.routes.forEach)
此错误是在我的 App.js 连接函数代码中触发的,该函数必须将一些状态变量映射到导航组件。这是我的代码:
NavReducer.js
import { NavigationActions, StackNavigator } from "react-navigation";
import routes from "../core/routes";
export const AppNavigator = StackNavigator(routes)
const routerForLogin = AppNavigator.router.getActionForPathAndParams('Login')
const routerForPlane = AppNavigator.router.getActionForPathAndParams('PlaneList')
const loginState = AppNavigator.router.getStateForAction(routerForLogin);
const registerState = AppNavigator.router.getStateForAction(AppNavigator.router.getActionForPathAndParams('Register'));
const planeListState = AppNavigator.router.getStateForAction(routerForPlane);
const insertState = AppNavigator.router.getStateForAction(AppNavigator.router.getActionForPathAndParams('Insert'));
export const navReducer = (state = { loginState, planeListState, insertState, registerState }, action) => {
switch (action.type) {
case '@@redux/INIT':
return {
...state,
planeListState: AppNavigator.router.getStateForAction(routerForPlane, loginState)
};
case 'LOGIN':
return {
...state,
planeListState: AppNavigator.router.getStateForAction(routerForPlane, loginState)
};
case 'LOGOUT':
return {
...state,
loginState: AppNavigator.router.getStateForAction(
NavigationActions.reset({
index: 0,
actions: [NavigationActions.navigate({ routeName: 'Login' })]
})
)
};
case 'REGISTER':
return {
...state,
registerState: AppNavigator.router.getStateForAction(action, state.registerState)
};
case 'INSERT':
return {
...state,
insertState: AppNavigator.router.getStateForAction(action, state.insertState)
};
default:
return {
...state,
planeListState: AppNavigator.router.getStateForAction(action, state.planeListState)
};
}
};
App.js
import React, { Component } from 'react';
import { createStore, applyMiddleware, combineReducers, compose } from 'redux';
import thunk from 'redux-thunk';
import { Provider, connect } from 'react-redux';
import { addNavigationHelpers } from 'react-navigation';
import authReducer from './src/reducers/LoginReducer';
import registerReducer from './src/reducers/RegisterReducer';
import planeReducer from './src/reducers/PlaneListReducer';
import insertReducer from './src/reducers/InsertReducer';
import Expo from 'expo';
import { AsyncStorage } from 'react-native'
import { navReducer,AppNavigator } from './src/reducers/NavReducer';
class App extends Component {
render() {
const { nav, dispatch } = this.props;
const state = AsyncStorage.getItem('token')
? this.props.nav.planeListState
: this.props.nav.loginState;
return (
<AppNavigator
screenProps={{ store: { store } }}
navigation={addNavigationHelpers({
dispatch: this.props.dispatch,
state: this.props.nav
})}
/>
);
}
}
const mapStateToProps = (state) => ({
nav: state.nav
});
const AppWithNavigationState = connect(mapStateToProps)(App);
const initialState = {
auth: { isLoading: false, error: null, username: '', password: '' },
insert: { wasInserted: false }
};
const rootReducer = combineReducers
(
{
nav: navReducer, auth: authReducer, register: registerReducer, planeList: planeReducer, insert: insertReducer
}
);
let store = createStore(rootReducer, initialState, applyMiddleware(thunk));
export default function Root() {
return (
<Provider store={store}>
<AppWithNavigationState />
</Provider>
);
}
Routes.js
import LoginComponent from "../components/Login";
import RegisterComponent from "../components/Register";
import PlaneListComponent from "../components/PlaneList";
import InsertComponent from "../components/Insert";
const Routes = {
Login: { screen: LoginComponent },
Register: { screen: RegisterComponent },
PlaneList: { screen: PlaneListComponent },
Insert: { screen: InsertComponent }
};
export default Routes;
我的代码有什么问题会触发该错误?谢谢。
根据您的代码,我认为问题出在您的 navReducer
我建议你将你的 navReducer 名称更改为另一个名称并组合它并创建新的 Reducer 名称 navReducer 像这样。
const navReducer = (state, action) => {
const newState = AppNavigator.router.getStateForAction(action, state);
// Simply return the original `state` if `nextState` is null or undefined.
return newState || state;
};
我还在 Github 上将我的 react-native-redux-example 推送到我的存储库 https://github.com/chen2584/react-native-redux-example
希望对您有所帮助!
刚刚经历了相同的错误消息:
在App
的render
方法中,你定义
const state = AsyncStorage.getItem('token')
? this.props.nav.planeListState
: this.props.nav.loginState;
但是 AppNavigator
中的 addNavigationHelpers
属性正在传递参数 state: this.props.nav
。
我的猜测是您打算传递之前定义的 state
(您当前未使用)。试试这个:
<AppNavigator
screenProps={{ store: { store } }}
navigation={addNavigationHelpers({
dispatch: this.props.dispatch,
state
})}
/>