React native:更改所选项目的颜色

时间:2019-04-29 13:13:32

标签: react-native navigation-drawer

我的抽屉导航是一个堆栈导航,这意味着我创建了一个自定义抽屉作为这样的堆栈导航:

class DrawerComponent extends React.Component {

  navigateToScreen = (route) => (
  () => {
    const navigateAction = NavigationActions.navigate({
      routeName: route
    });
    this.props.navigation.dispatch(navigateAction);
});


  render() {

    return (
          <ScrollView style={Styles.containerDrawer}>
            <View style={Styles.logoContainer}>
              <Image source={Images.logo}
                style={Styles.imageStyle}
                resizeMode={'contain'}
              />
            </View>

            <View style={Styles.blocksContainer}>
              <View style={Styles.firstBlock}>
                <TouchableOpacity
                  style={[Styles.buttonMenue, Styles.elevationButton, Styles.bgButton, Styles.centerContent]}
                  onPress= {this.navigateToScreen('Messages')}
                >
                  <IconMCI
                    name="message-text-outline" size={wp('10%')} color= '#000'
                    style={Styles.iconStyle}
                  />
                  <Text style={Styles.textButton}>الرسائل</Text>
                </TouchableOpacity>
                <TouchableOpacity
                  style={[Styles.buttonMenue, Styles.elevationButton, Styles.bgButton, Styles.centerContent]}
                  onPress= {this.navigateToScreen('Home')}
                >
                  <IconSI
                    name="home" size={wp('10%')} color= '#000'
                    style={Styles.iconStyle}
                  />
                  <Text style={Styles.textButton}>الإستقبال</Text>

                </TouchableOpacity>
              </View>

我的问题是我无法更改所选商品的样式。

这是我的抽屉,看起来像这样: enter image description here

3 个答案:

答案 0 :(得分:0)

您可以使用状态来处理项目的点击事件并根据其更改类,下面提供了示例示例

    class DrawerComponent extends React.Component {

      state={
      clicked:true;
      }

      navigateToScreen = (route) => (
      () => {
        const navigateAction = NavigationActions.navigate({
          routeName: route
        });
        this.props.navigation.dispatch(navigateAction);
    });


      render() {

        return (
             ...

                <View style={Styles.blocksContainer}>
                  <View style={Styles.firstBlock}>
                    <TouchableOpacity
                      style={!this.state.clicked?[Styles.buttonMenue, Styles.elevationButton, Styles.bgButton, Styles.centerContent]:['custom-style']}
                      onPress= {()=>{
    this.setState({clicked:true})
    this.navigateToScreen('Messages')
    }}
                    >
 ...
                    </TouchableOpacity>

                  </View>

答案 1 :(得分:0)

this.props.activeItemKey方法内的

render将为您提供当前的项目密钥,例如MessagesHome,您可以使用...来设置活动项目的样式相应地。

第二

如果可能,将StackNavigator在routeConfigs中呈现的屏幕移到抽屉中:

const DrawerNavigation = createDrawerNavigator({
  Home: { screen: HomeScreen },
  Screen2: { screen: StackScreen2 },
  Screen3: { screen: StackScreen3 },
});

答案 2 :(得分:0)

这是更多信息:

import React, {Component} from 'react';
import { createStackNavigator, createDrawerNavigator, createAppContainer } from 'react-navigation';
import HomeStackNavigation from './HomeStackNavigation'
import Messages from '../Components/Messages/Messages';
import DrawerComponent from '../Components/Drawer/Drawer';
import {Platform, Dimensions} from 'react-native';
// import ScoreList from '../Components/ScoreList/ScoreList';

var width = Dimensions.get('window').width;

const DrawerNavigation = createDrawerNavigator({
    Home: { // entree (route name) : on peut la nommer comme on veut mais on prefere lui donner le meme nom que notre screen qu'on va afficher
      screen: HomeStackNavigation, // le screen qu'on va afficher IL DOIT ETRE UN STACK
    },
},
{
  drawerWidth: width*0.83,
  contentComponent: props =>
  {
    return(<DrawerComponent  {...props}/>)
  },
  drawerPosition: 'left',
},
);

export default createAppContainer(DrawerNavigation)