7

我正在学习 React Native 和 Redux,我已经开始使用 3rd 方库——特别是React Navigation

我已经按照Dan Parker 的 Medium Tutorial上的教程进行了操作,但仍然无法正常工作

我的应用程序的 RootContainer:

<PrimaryNavigation />
...

export default connect(null, mapDispatchToProps)(RootContainer)

PrimaryNavigation 定义:

const mapStateToProps = (state) => {
  return {
    navigationState: state.primaryNav
  }
}

class PrimaryNavigation extends React.Component {
  render() {
    return (
      <PrimaryNav
        navigation={
          addNavigationHelpers({
            dispatch: this.props.dispatch,
            state: this.props.navigationState
          })
        }
      />
    )
  }
}

export default connect(mapStateToProps)(PrimaryNavigation)

PrimaryNav 定义:

const routeConfiguration = {
  LoginScreen: {
    screen: LoginScreen
  },
  MainContainer: {
    screen: MainContainer
  }
}

const PrimaryNav = StackNavigator(
  routeConfiguration,
  {
    headerMode: 'none'
  })

export default PrimaryNav

export const reducer = (state, action) => {
  const newState = PrimaryNav.router.getStateForAction(action,state)
  return newState || state;
}

我的创建商店:

const rootReducer = combineReducers({
  ...
  primaryNav: require('../Navigation/AppNavigation').reducer
})

return configureStore(rootReducer, rootSaga)

我收到以下错误:

Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. Check the render method of 'PrimaryNavigation'

到目前为止我的理解是:

  • RootContainer 连接到一个商店——它拥有一个 PrimaryNavigation
  • PrimaryNavigation 包含一个 Navigator (PrimaryNav),它包装 Navigator 并传递它的状态
  • PrimaryNav 是实际的导航器 - 我已经定义了路由和默认初始化
  • 处理 PrimaryNav 的减速器只是 PrimaryNav.router.getStateForAction

我错过了初始状态吗?我没有将它正确连接到 Redux 吗?我是否需要触发调度才能进入第一个屏幕?

谢谢

4

2 回答 2

1

我认为您在代码组织中遗漏了一些东西。

这是我为扩展 Tyler Mcginnis 所做的 github notetaker 应用所做的努力。我更新了代码以支持最新版本的 React 和 React Native,我还扩展了应用程序以使用支持 iOS 和 Android 的 react-navigation。我添加了 Redux 来管理存储,并使用 Redux-Sagas 来处理数据的异步获取。

https://github.com/ahmedkamohamed/react-native-navigation-redux-sagas

于 2017-06-20T23:17:50.193 回答
0

你可以从我的 git 看到完整的演示:https ://github.com/liuxiaocong/redux-with-react-navigation 使用 redux 和 react-native 中的 react-navigation

于 2017-08-30T06:03:53.900 回答