I am working on react native 0.62 in which i have used Drawer Navigator. when user logged into the app i am storing auth api response into the redux store. After login, i wanted to display logged in user name in the dashboard header but i am unable to use redux store inside my function. Tried with hooks but not worked. Any help or suggestion. Thank You in advance. Here is my code:
const GradientHeader = props => (
<View style={{ backgroundColor: 'transparent' }}>
<LinearGradient
colors={['#6CCFF6', '#596AB2']}
start={{ x: 0, y: 0 }} end={{ x: 1, y: 0 }}
>
<Header {...props} />
</LinearGradient>
</View>
)
const DashboardStackScreen = ({ navigation }) => {
return (
<DashboardStack.Navigator screenOptions={{
headerTitle: 'Good Morning, John', // i wanted to access the redux store here
header: props => <GradientHeader {...props} />,
headerLeft: () => (
<TouchableOpacity onPress={navigation.openDrawer} style={{padding: 20}}>
<Image source={require('../assets/images/menu_bar.png')} style={{width:18, height:12}}/>
</TouchableOpacity>
),
headerTransparent: true,
headerStyle: {
backgroundColor: 'transparent'
},
headerTintColor: '#fff',
headerTitleStyle: { fontFamily: 'OpenSans-SemiBold', fontSize: 20},
}}>
<DashboardStack.Screen name="Dashboard" component={Dashboard} />
</DashboardStack.Navigator>
);
}
const MainNavigator = ({navigation}) => {
return (
<Drawer.Navigator initialRouteName="Dashboard" drawerContent={(props) => <DrawerContent {...props} />} hideStatusBar={false} focused={true} labelStyle={{ fontSize: 14, fontFamily: 'OpenSans-SemiBold' }} drawerContentOptions={{ activeBackgroundColor: "#F1F1F1", activeTintColor: "#000000", inactiveTintColor: "#818181",itemStyle: { marginLeft:0, paddingHorizontal: 10, width:'100%', borderRadius: 0}}}
>
<Drawer.Screen name="Dashboard" component={DashboardStackScreen} options={{
drawerIcon: ({ focused, size }) => (
<Image source={require('../assets/images/dashboard.png')} style={{ height: 17.78, width: 16}} resizeMode="contain"/>
)
}}
/>
</Drawer.Navigator>
);
}
const mapStateToProps = state => {
return {
data: state
};
};
export default connect(mapStateToProps)(MainNavigator);
login.reducer.js import { AUTH_REQUEST, AUTH_SUCCESS, AUTH_FAILURE, SET_AUTH } from '../utility/Constants';
const INITIAL_STATE = { user: {}, loading: false, error: '', //false isAuthorized: false };
const login = (state = INITIAL_STATE, action) => { switch (action.type) { case AUTH_REQUEST: return Object.assign({}, state, { loading: true, user: {}, error: '',//false isAuthorized: false }); case AUTH_SUCCESS: return Object.assign({}, state, { user: action.payload, loading: false, error: '',//false isAuthorized: true }); case AUTH_FAILURE: return Object.assign({}, state, { user: {}, error: action.payload,//true, loading: false, isAuthorized: false }); case SET_AUTH: return Object.assign({}, state, { error: '',//true, loading: false, }); default: return state; } };
export default login;
react-redux
<Provider>
properly set up? – Serhii Yukhnevychcodesandbox
? – bakar_dev