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:
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
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.
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 .
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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With