开发者问题收集

如何在函数组件中渲染 redux 存储值?

2020-07-30
732

我正在使用 React Native 0.62,其中我使用了 Drawer Navigator。当用户登录应用程序时,我将 auth api 响应存储到 redux 存储中。登录后,我想在仪表板标题中显示登录的用户名,但我无法在我的函数中使用 redux 存储。尝试使用钩子但没有成功。任何帮助或建议。提前谢谢您。 这是我的代码:

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;

3个回答

如果您已使用 Provider 连接到商店,那么您可以使用 props 作为数据访问组件 MainNavigator 中的数据,因为 mapStateToProps 返回具有您的状态的数据。

const MainNavigator = ({navigation, data}) => {
   console.log('Redux store data', data);
   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>
  );
}
Darshita Golchha
2020-07-30

您可以在组件中想要显示的任何位置使用 props.data,请尝试这样的 headerTitle: props.data

Abhishek
2020-07-30

我没有使用 DashboardScreenStack 中 mapStateToProps 的存储值(这种方法不起作用),而是直接从基于函数的组件中调用 store.getState()。它可以满足我的需要。

Tanu Sharma
2020-08-03