【问题标题】:custom drawer focused from @react-navigation/drawer is not working correctly来自@react-navigation/drawer 的自定义抽屉无法正常工作
【发布时间】:2020-12-17 00:06:37
【问题描述】:

我正在构建需要自定义抽屉元素的应用。我希望能够突出显示当前选择的屏幕。我使用 Context 来保持当前屏幕的状态被选中,并且它确实发生了变化。 changeDrawerElem 是在 Contex 中保留元素编号的函数,我看到它在 console.log 中更改了 onPress。但是,无论我试图控制它们,CustomDrawerContent 的元素都会被渲染。在文档中,据说属性“focused”应该采用布尔值并基于突出显示的元素。反应导航版本:5 任何帮助表示赞赏。

import React, { Component } from 'react';
import { useContext } from 'react';
import { Context } from '../context/settingContext';
import { StyleSheet, Text, View, ImageBackground, TouchableOpacity} from 'react-native';
import { DrawerContentScrollView, DrawerItemList, DrawerItem } from '@react-navigation/drawer';
import FontAwesome, { SolidIcons, RegularIcons, BrandIcons } from 'react-native-fontawesome';
import defaultStyles from '../css/defaultStyles';

function CustomDrawerContent (props){

    const {state} = useContext(Context);
    const {drawerElem} = state.find(elem => elem.hasOwnProperty('drawerElem'));
    const {changeDrawerElem} = useContext(Context);


    return(
        <DrawerContentScrollView {...props}>
            <DrawerItem
                label="Головна"
                onPress={() => {
                    changeDrawerElem(1);
                    props.navigation.navigate('StackNav', { screen: 'MainScreen'} )
                }}
                icon={() => <FontAwesome style={styles.drawerIcons} icon={SolidIcons.home} size={30} />}
                inactiveTintColor = "#000000"
                activeBackgroundColor = "#cdb9a5"
                focused = { () => drawerElem == 1 ? true : false}
             />
             <DrawerItem
                label="Зміст"
                onPress={() => {
                    changeDrawerElem(2);
                    props.navigation.navigate('StackNav', { screen: 'Content' });
                }}
                icon={() => <FontAwesome style={styles.drawerIcons} icon={SolidIcons.listOl} size={30} />}
                inactiveTintColor = "#000000"
                activeBackgroundColor = "#cdb9a5"
                focused = { () => drawerElem == 2 ? true : false}
              />
              
        </DrawerContentScrollView>
    );
}

const styles = StyleSheet.create(defaultStyles);

export default CustomDrawerContent;

【问题讨论】:

    标签: react-native react-navigation react-native-navigation


    【解决方案1】:

    我找到了一个解决了我的问题的答案,你需要将你的焦点道具更改为

    focused={props.state.index ===  props.state.routes.findIndex(e => e.name === 'Головна')}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多