Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React-navigation Redux State management

I'm learning React Native and Redux and I've started using 3rd party libraries - specifically React Navigation

I've followed a tutorial on it Dan Parker's Medium Tutorial and I still can't get it working

My RootContainer of the app:

<PrimaryNavigation />
...

export default connect(null, mapDispatchToProps)(RootContainer)

PrimaryNavigation definition:

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 definition:

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;
}

My create store:

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

return configureStore(rootReducer, rootSaga)

I get an error along the lines of:

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'

My understanding so far is:

  • RootContainer is connected to a store - it holds a PrimaryNavigation
  • PrimaryNavigation contains a Navigator (PrimaryNav), it wraps the Navigator and passes it state
  • PrimaryNav is the actual Navigator - I've defined routes and default initializations
  • the reducer that handles PrimaryNav is just PrimaryNav.router.getStateForAction

Am I missing initial state? Am I not connecting it to Redux properly? Do I need to fire off a dispatch to go to the first screen?

Thanks

like image 333
myTotoro Avatar asked Apr 03 '17 13:04

myTotoro


People also ask

Can I store the navigation state in Redux too?

Can I store the navigation state in Redux too? ​ This is not possible. We don't support it because it's too easy to shoot yourself in the foot and slow down / break your app.

What is redux integration?

Redux is a state management library that produces a global state for the entire app so that it can be accessed by all components. It is important that you have one global state for the entire app and not separate states for each component. You will need two actions: startAction.js and stopAction.js .

How easy is it to use Redux with react navigation?

It is extremely easy to use Redux in an app with React Navigation. It's basically no different than without React Navigation. Notice that we wrap our components in a Provider like we'd normally do with react-redux.

What is appwithnavigationstate in react navigation?

It also sets up AppWithNavigationState, a top-level container holding the navigation state. If this seems unclear, don’t worry. This is standard boilerplate code in React Navigation and we’ll just use it for now to get things going. Time to write the navigation reducer, which will hold the navigation state inside the Redux store.

Can I store the navigation state in Redux too?

Create a component, connect it to the store, then use that component in the title. If the value isn't expected to change, you can just pass it from a connected component to the other screen as a param. So our component will look like this: Can I store the navigation state in Redux too? This is not possible.

What is React-Redux package used for?

The package redux is framework agnostic and will connect your actions and reducers. The package react-redux contains the bindings to run a Redux store in a React project.


Video Answer


1 Answers

I think you are missing a few things in your code organization.

This is my effort to extend the github notetaker app done by Tyler Mcginnis. I updated the code to support latest versions of React and React Native, I also extended the app to use react-navigation supporting both iOS and Android. I added Redux to manage the store and used Redux-Sagas to handle asynchronous fetch of data.

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

like image 135
Ahmed Khaled Mohamed Avatar answered Nov 05 '22 19:11

Ahmed Khaled Mohamed