I am having an app in which I am using 3 Context Provider
. For the app to work, I have to wrap <App/>
in all these providers
. As my app grows I am expecting to have a few more providers for more types of data I have to connect with. I have already started to feel that there may be a better way to pass providers into the <App />
.
My App.js
Code:
import React from 'react';
import { createStackNavigator } from 'react-navigation-stack';
import { createAppContainer } from 'react-navigation';
import { Provider as BlogProvider} from './src/context/BlogContext';
import { Provider as VehicleProvider} from './src/context/VehicleContext';
import { Provider as AuthProvider} from './src/context/AuthContext';
import IndexScreen from './src/screens/IndexScreen';
import ShowScreen from './src/screens/ShowScreen';
import CreateScreen from './src/screens/CreateScreen';
import EditScreen from './src/screens/EditScreen';
import VehicleScreen from './src/screens/VehicleScreen';
const navigator = createStackNavigator(
{
Index: IndexScreen,
Show: ShowScreen,
Create: CreateScreen,
Edit: EditScreen,
Debug: DebugScreen,
Vehicle: VehicleScreen,
},
{
initialRouteName: 'Index',
defaultNavigationOptions: {
title: 'Main'
}
}
);
const App = createAppContainer(navigator);
export default () => {
return (
<BlogProvider>
<VehicleProvider>
<AuthProvider>
<App />
</AuthProvider>
</VehicleProvider>
</BlogProvider>
);
};
Some of the questions I have are:
- Is there a better way to use multiple context providers in an App.
- Does the order in which these providers are nested have any impact on the App?
- Can we skip adding provider in
<App/>
and instead import them in a any screen where it's required and wrap that screen element in it?